Reference (text) files in compiled MacOS app (application file) - c#

I'm working with Avalonia to create a MacOS desktop program using c#. I can run the program locally or output it to a final app file using dotnet.bundle. Attached image shows the contents of the app file using Mac's "show package contents".
There are files that are deployed to the app bundle that I would like to read when running the app, but don't know how to reference them. Since this is in C#, I can't find any docs similar to what apple provides here https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW1
I've tried referring to the parent directory, but this only works in design time.
I've tried a variety of syntax including Environment.GetCurrentDirectory as prefix to the below path.
string path = "Repository/CreateTables.sql";
string assets = File.ReadAllText(path);
Thanks

Thanks for the tip, yes I have been trying to print out paths but oftentimes they were blank when run from the .app (for example, Environment.CurrentDirectory returns ""). I did find a way to get at the path here https://github.com/AvaloniaUI/Avalonia/issues/5443#issuecomment-775906396, and it was not the path I was expecting. I thought the location of the file would start at myapp.app, but you need the path to myapp.app as well.
/Applications/MyApp.app/Contents/MacOS/Repository/CreateTables.sql

Related

Add Tesseract to the output directory C#

This is starting to annoy me. I have a UWP that allows you to open up a separate windows forms app to take advantage of tesseract. The app works fine when running on its on, however as soon as I call upon it to run using the UWP, the app crashes and gives me the error that it could not find the file specified, that file being the tesseract folder. I have it referenced within the app as
var path = "C:\\Users\\dan\\source\\repos\\TesseractApp\\packages\\Tesseract.4.1.1";
Is this something really simple I'm missing? If anything I'd like to copy the Tesseract.4.1.1 folder to directory, but that doesn't seem to work either. Hopefully this is clear.
EDIT - I believe it is just the exe file not working properly. I think I'm not copying over enough files into the Assets folder of the UWP and the exe of the tesseract app does not have what it needs to work.

Visual Studio File Path Windows vs. Linux

I have some confusion about Windows path (dev machine) and Linux path (target OS for microservice)
I created a new folder in my c# project (app/yamls), put inside a file aaa.yaml and put the file in git ignore. On my windows dev machine, I see the file in c:\myproject\app\yamls. However, my microservice will run on Linux and aaa.yaml file will reside in /app/yamls.
When I run the following, I get file is missing
string file = "/app/yamls/aaa.yaml";
System.IO.File.Exists(file) // FALSE
What should be done so the new added file in visual studio will be seen as file residing in /app/yamls like it will be in production.
Thanks
You need to code file and directory handling in a cross platform manner.
You could something like this: https://learn.microsoft.com/en-us/dotnet/api/System.IO.Path.PathSeparator?view=net-5.0&viewFallbackFrom=netcore-5.0
That System.IO.Path.PathSeparator will be / on Linux and \ on Windows
What I usually do is create an appsettings.json file with file location in there. And then change the config file depending on where it's deployed (Linux or Windows)
Path class has static members useful if manipulation of file paths required in .Net environment. Look at these:
Path.DirectorySeparatorChar
Path.AltDirectorySeparatorChar
Path.PathSeparator
Path.VolumeSeparatorChar
If you just need to create path from given components the Path.Combine(...) method is also useful.

how to set image in Visual Studio C# Windows Forms with Image.FromFile

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

How to get the true startup path at runtime of a c# console aplication

I created a console app, I put its shortcut in sendto directory.
Now I select in some OTHER directory a list of files to sendto shortcut and want to know what this other directory is.
I tried many things like Directory.GetCurrentDirectory and Environment.CurrentDirectory but it returns the path of my exe not the other directory where I selected my files.
So do you know how ?
You need to look at the pathname(s) of the files being passed to your application by Windows.
From Windows PoV, there's nothing special about the directory the files are in; Windows is just passing the path of those files to your app. As the commenter above says, you can use members of the Path class to manipulate the paths and extract a directory name, if that's what you want.
There are three separate concepts here:
The directory where your .EXE is (doesn't change during execution)
The 'current' or 'working' directory (often the same as '1', but not necessarily) - may change while running - initially is under control of whoever launched your app.
The directories where files your application is working with reside - this could be many different things at different times in the lifetime of the app.
AppDomain.CurrentDomain.BaseDirectory
Add to reference Windows.Forms:
using System.Windows.Forms;
and:
Application.StartupPath

Image.Save to relative path doesn't save where expected

The documentation doesn't explain the behavior when passing in a path such as "myFile_temp.jpg" but I would assume that it would save the the application directory because this is a relative path, relative to the application we are currently running.
I think that the problem can be solved by prepending the current application directory to my temp file name using
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Sure there are lots of ways to do it, but this should work.
My issue is I'd like to know why this is happening rather than just throw a patch on it and ship it back out to the users.
Code is WPF, C# project compiled with .NET 4.0 and Visual Studio 2010 and runs on a lot of different machines. Mostly 32-bit XP,while the dev machine is a 64-bit Windows 7.
Can any one explain this behavior and why it's occuring?
Edit
The files will on occasion be saved to the directory the user selected files from to manipulate. They resize them, the program keeps track of the size percent for each of the file paths. When the user is finished they will click done and the program will go through each of the file paths, create a copy, resize the image and then save it with a _temp on the end.
Take note that it doesn't always do it and it when it does it doesn't do it for all the files they touched.
It works as s expected. You just didn't expect valid behavior. Lets assume that your app is placed in c:/superapps/myapp.exe. You opened command line and you're in C:\ which means that this your current working directory.
You can still run your app by ./superapps/myapp but your working directory is still C:\. And this will be working directory of your app in this case, not the directory you placed the binaries.
That is why it may not have permission or save data in some unexpected by you location. You should always think that your app could be run just like any other command like dir. It will be working in the place where user is currently standing (his current working dir) not in the place it's binaries are stored in

Categories

Resources