I'm relatively new to C#, so pretty much I have a folder included in my WinForms project that contains a custom font. The font works absolutely fine upon launch of the application, but in the actual designer on Visual Studio itself, the custom control can't be loaded (as the path I'm using is different when in the designer). Here is the code I use to locate the font from the resources folder:
string leadDirectory = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Resources" + Path.DirectorySeparatorChar + "Roboto-Regular.ttf");
The directory which is used in the designer is "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Resources\Roboto-Regular.ttf", and obviously it throws a file not found exception in the method.
Any help would be appreciated!
You are not opening the directory you think you are opening. Check in debug mode what System.Windows.Forms.Application.StartupPath actually is.
To fully understand what goes wrong here, you should understand how running programs from the command prompt work. You have the command prompt, and on that prompt, you see your "current path" is set to a certain directory. Normally, this indicates you want to do stuff in that particular directory, but you can launch a program that is anywhere on your system by giving the full path to launch. However, this will not make your command prompt switch to the path of that launched application. You will still remain in that same folder, despite running a program that is located somewhere else. This folder is the "startup path" that you are using. As you can imagine, it has no relation at all to where you are trying to look for that Resources folder.
Despite having evolved to a graphical user interface, the way programs launch still works the same way as it did in DOS, so this distinction remains.
In Windows Forms, you can use Application.ExecutablePath to get the full path and filename to your exe file, so if you use Path.GetDirectoryName() on that, you have the base path you want. In case your program would not be a WinForms application, you can instead use Assembly.GetExecutingAssembly().Location, from the System.Reflection namespace.
Related
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
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.
So, I have completed a C# console application using Microsoft Visual Studio 2012.
I have a local file within the project folder. Within my code, I have performed a log writing operation by assigning the relative path of this file to a string variable. The file was created during the first build/run and then appended with information during further runs.
string rpath = "..\\LogFile2.txt";
I built this project in debug mode and ran it. During this run the rpath is correctly identified as
c:\project_app_folder\bin\LogFile2.txt
However, while building this project in release mode and then scheduling the .exe file to run at a particular time in the windows task scheduler, I get a run-time error saying
Could not find a part of the path 'C:\local\pic'.
How do I resolve this? I want the temporary folders or text files created during run time to be part of my project folder/package?
Please also note , I cannot put absolute paths as this code will have to be packaged and sent to another user and that user may chose to store the program in a location he/she sees fit.
This issue is not because of release mode, when the scheduler invokes the program the working directory will be different as #Damien_The_Unbeliever said. you can use the "start in" option when you specify your action, as your release directory.
The "start in" is mainly to make sure that if you have relative paths
in the task to run, it understands which directory to use.
How can I run an embedded .exe file (installer) in C# Windows Forms applications the easiest way?
I just want to click a button and an installer should open. My .exe file's name is setup.
If I try Process.Start(setup.exe); I get an error:
The name 'setup' does not exist in the current context
And if I try
System.Diagnostics.Process.Start("setup");
it will open folder C:\Windows\System32\setup.
If what you expect is the easiest way, then don't embed setup.exe as a resource, but as Content.
(Add setup.exe to your project, and right click on setup.exe in Solution Explorer to edit properties, set as content, and select Copy if newer.)
Another option if setup.exe is a project of your Visual Studio solution, is to automatically copy setup.exe in the output directory of the launcher project: Add a reference to setup.exe if it belongs to the solution, so it is automatically copied every time you compile and there was a change.
Last is the code that is pretty easy - you already have it, actually:
System.Diagnostics.Process.Start("HelloWorld.exe");
If needed, you can change the current directory:
Environment.CurrentDirectory = #"c:\someSetupExeDir";
But better, you can use Path.Combine:
String fullPath = Path.Combine(directoryPath, fileName);
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