I am trying to create a Bitmap using:
bitmap = new Bitmap(#"Movies\View\Images\missing_person.bmp");
However, I am receiving a System.ArgumentException error.
The file I'm calling the above code from is located in:
MyProj\DisplaySideBarCommand.cs
The images are in:
MyProj\Movies\View\Images\missing_person.bmp
I also tried using:
bitmap = new Bitmap(#"..\Movies\View\Images\missing_person.bmp");
but received the same error.
It is going to look for the files relative to the executing assembly. When you build your project it is probably output to a directory like bin\debug or bin\release. You could build your relative path to backtrack from there, or you could copy the files to the output directory.
If you set the build action to Content on the files, they will be copied to the output directory (including sub folders) on build and then you should be able to build the correct relative path from there.
If you are using the default settings, the debug binaries would be in ProjectDirectory\bin\Debug\ - therefore, you might want to try #"..\..\Movies\View\Images\missing_person.bmp"
This is because the images are located in your project folder, not your output folder.
string projectFolder = "..\\..\\"; // Goes back to the project folder
Once you got the projec path simply use it like this:
bitmap = new Bitmap(projectFolder + #"Movies\View\Images\missing_person.bmp");
I would suggest you to move your files to the output folder rather than storing them in your project. Since normally you only distribute the output folder and not your whole project (Unless it's open source of course.)
Related
the problem that i have with my application is about how to reference some files, when i load and image, etc, i use Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\")); and this works fine for debugging and for some files, but doesn't work for some essential files, credentials.txt, config.ini, this files are searched in appdata when i run the published file and i don't know how i should reference them.
I try to generate some initial files so i am sure where they are and that they exists, but for folders that works great with
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
with that i can make the folders where the app is run but you can't generate a file without load the content of the original file, and i don't know how to reference them.
Example:
Project.
Root of the project.
utils/key/credentials.txt
When the published file is executed i want to generate and store credentials.txt like this:
Executable (app.exe stored for example in "my documents")
"my documents"/utils/key/credentials.txt
how i do that? when i run the published application you just have the .exe, the dlls, and the resources are embebbed, so the uri doesn't works.
Assuming you have sufficient permissions, you could create a folder in the output folder of your compiled .exe at runtime using the Directory.CreateDirectory method.
If the utils/key/credentials.txt file is part of your deployment, you should set its Build Action to Content and the Copy to Output Directory property to Copy if newer in Visual Studio. This will add the utils and key folders to the output directory of the .exe, which is typically c:<project-folder\bin\Debug or \Release when you build from Visual Studio.
You can get the absolute path of the output directory like this:
string path = System.IO.Path.GetDirectoryName(
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Depending on your requirements, you may then append the relative path of the file within your app to the absolute path.
I have windows form application and i have some text files in folder as in image. How can i reach them and read that files?
I also tried code below but get error value cannot be null. parameter name stream
Assembly asm = Assembly.GetExecutingAssembly();
StreamReader reader = new StreamReader(asm.GetManifestResourceStream("NexoClientApp.JsonRequests.Login.txt"));
string jtext = reader.ReadToEnd();
Thanks
be sure to copy the files while building!
See the properties of your text files like in this example:
(copy if newer will also work fine)
If you want the files to be installed as files in the destination system by keeping the project structure, you need to read them as disk items, and create a setup or simple zip that copy these files to restore the project structure for needed items.
To read them, in case of the executable is generated in bin folder as by default for every VS project:
var lines = File.ReadAllLines(RootFolderPath + #"JsonRequests\Login.txt");
public string RootFolderPath
= Directory.GetParent
(
Path.GetDirectoryName(Application.ExecutablePath.ToLower()
.Replace("\\bin\\debug\\", "\\bin\\")
.Replace("\\bin\\release\\", "\\bin\\"))
).FullName
+ Path.DirectorySeparatorChar;
We remove the debug or release folder to get the project/app root path to be able to read the desired file.
If the binary is generated in another folder, use it. If in the root itself, use it as-is. If you change to have the save folder for release and debug, adapt that.
In WinForms, all methods to get the executable path returns a path having by default debug or release... but here we need the root path of the project or installed app.
Here we don't set copy files to the executable folder in the solution explorer, but we keep the project structure on disk.
You can also create a ressource file to embbed the files you want and use GetManifestResourceStream or use the #FalcoAlexander answer to copy files in the executable folder and read from there.
string dir = AppDomain.CurrentDomain.BaseDirectory;
//OR
string dir = Application.StartupPath;
string login = File.ReadAllText(dir+"/JsonRequests/Login.txt"); //read some file
//OR
List<FileInfo> files = new DirectoryInfo(dir).GetFiles().ToList(); //get all info about files in root dir
I want to acess a .txt file, which I stored in the resources folder of my project, there where all the imported pictures are stored as well.
I want to use something like a relative path, since every user would have safed his programm somewehere else on his Pc, but there is always the resources folder at the same place, compared to the programm folder.
I tried to used this: displayText = System.IO.File.ReadAllText("Resources\\startmessages.txt"); but this isn't working.
I get this error message: System.IO.DirectoryNotFoundException:, it lists the unrelative path to the .txt there as well, so I don't get, why it cant read it.
Thanks for your Help.
What #ChetanRanpariya is trying to tell you is, that your programm is built in another folder than your folder Resources is sitting to. So you have explictly tell your file Resources\startmessages.txt to copy itself on build process, so it get copied to said another folder. Assuming that you are using Visual Studio, you have to right click on your file and set Copy To Output Directory to true. Its relative folder path (Resources\) will be taken over. You find your build folder somewhere in your bin folder depending on configuration and framework. :)
Current Path where your executable is
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).FullName
Path to Solution
If you are using Visual Studio and need to access the folders in the solution directory, you can use .Parent method,
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName
Use of Path.Combine
and once you have the location of your Resource folder, use Path.Combine to get the location to read files / content etc
Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Resources\\startMessages.txt")
I need to read data from a file in a c# console application.
What works: new StreamReader(#"..\\..\myData.csv");
Problem:
the ..\\..\ work because my exe file is in the bin/Debug directory
When I deploy my project the path doesn't work any longer
Question:
How can I reference myData.csv regardless of the location of the exe file?
I had hoped to find a method that returns the 'root' of my console application
So far I tried the following:
Process.GetCurrentProcess().MainModule.FileName
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location
Path.GetFullPath("bp.csv")
AppDomain.CurrentDomain.BaseDirectory
Directory.GetCurrentDirectory()
All of these expressions lead me to the directory of the exe file not the root.
I just started to read about isolated storage but it would be nice to have something simpler. Any suggestions / recommendations?
The simplest option is probably to add your CSV file to the solution and right-click it in VS and set the build action to "Copy if newer", which will output it together with the .exe (to the Debug or Release folder) when you build.
In the code, you can get the current location of the executing assembly like this:
string folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
And then you can combine the path with the CSV file name:
string filePath = Path.Combine(folder, "myData.csv");
Where your myData.csv will be stored ? You should have an absolute location of this file.
there are couple of options
You can place this file at the same directory where your exe is placed so you will only need to do
new StreamReader("myData.csv");
you can define file location in the App.Conig file and read that location.
you can set a path variable an read the PATH variable.
You should change your code to
new StreamReader("myData.csv");
This will ensure that the data is always read from the same folder the .exe is run from.
After that, you can create a post build step to copy the file to the deployment folder (or a subfolder) so that even in your debug environment the file will be in the correct place. The property "Copy to Output Folder" on the data file will do this as well if you just need the file to be in the output path for a project.
If you need more control, n the post build steps you can use macros like $(ProjectPath) to reference where the project files are located and $(TargetDir) to reference where the output directory will be.
I have a C# project where I include some resources which I need to refer using the Uri-class. For this specific problem I have some shaders placed in a "Shaders" folder in the root of the project, but I've had this problem before with other files like images, etc. This far I've used the simple solution giving a fixed absolute path, and making sure the file is present at that location. Needless to say - this is not a good solution, and it won't work in the long run...
So, how can I use relative paths to refer my resources? I guess my question is twofold:
First; I don't want to refer the relative path from my Debug folder to the project folder. I want the file to be copied to the build folder. The shaders are included in the project, but obviously this isn't enough. How do I tell the project to copy the files when building? Or; what's the common way of solving this?
Second; when I have the "Shaders" folder copied to my build folder - what Uri syntax do I use to refer e.g. "myShader.ps" placed inside this folder? Can I simply say file:///Shaders/myShader.ps?
To get a Uri from your applications directory do:
Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
Uri shader = new Uri(baseUri, "shaders/test.ps");
Answer to first:
How do I tell the project to copy the files when building?
Add the file to the project, right click, select Properties and change Copy to Output Directory to Always Copy.