I have a application with one folder which i added by right clicking the project, selecting add folder. Inside this folder i have xml files which are set to build action:content, copy to output directory: copy if newer (i have tried setting to embedded resource) As well as this i have a few text files and so on.
In my bin/debug output directory i have the exe, the folder with the xml, the stand alone .txt files and so on. My problem is, if i send the exe to my friend to try he always gets an exception thrown.
Say he puts the exe on the desktop, my programme at some point reads the filenames of the xml files in the folder. It uses the following code to do so
String[] filePaths = Directory.GetFiles(#"DataSources\");
I assume that because of this, when the exe runs from the desktop, it expect the folder of .xml files to be in the same place? I have the same type of exception when trying to read the .txt files too. What am i doing wrong here?
Thanks for your time
When reading from files using relative paths you get the one relative to the applications current directory. tip: In C# you can see what directory that is using Environment.CurrentDirectory.
So if you create a shortcut on your desktop, you need to make sure you right click the shortcut and set its "Start in"-folder to the directory of your application. That way its current directory will be set when its started and relative paths will be relative to that path and not the path of the shortcut.
If you actually moved the exe file to the desktop you also need to move any resources that it needs, so if it wants a folder named "datasources" you would have to move that folder as well, or set the current directory when you start the application.
Have you tried something like: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx or http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx ?
So
Directory.GetFiles(environment.currentdirectory + #"\DataSources\");
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 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")
What exactly is Working Directory in the properties of Visual Studio C# project.
I have see a project where I right click and go to Properties and then I go to Debug tab, it shows me Working Directory where Author of the code has specified folder in local machine. I want to know what does that working directory means and what kinds of file will it store in that directory.
Thank you in advance.
Every process has a current Working Directory which is where all relative paths will be formed from. If not specified, this directory is the directory in which the active application started.
You can check which directory is set by calling:
System.IO.Directory.GetCurrentDirectory();
As mentioned above in a comment by #0xA3 this setting has no effect to your deployed product, it is is only for debugging.
The working directory of a project (and any Windows program in general) is the default place in which a program is looking up it's files. For example: My program has working directory C:\dir and tries to open test.txt, it would look in C:\dir\test.txt.
So every opened file will be opened relative to the working folder.
I think it will store nothing there, unless you add/write code in your application which explicitly creates files, either explicitly in the application's working directory, or implicitly specifying only a filename without specifying a directory at all.
I am developing an application in C#. I have a folder called myfolder it contains a file called mypoints.bmp. The folder myfolder is in my project folder it is in D drive. The path is D:\Myproject\myfolder\mypoints.bmp.
Now, in my program, whereever I need mypoints.bmp I have hardcoded the whole path. When my project is copied in to a different system under C drive, I am not able to run my project because of the hardcoded path. How can I give the path so that there will not be any problem even if it is loaded in to a different system under a different drive.
The best would be to store the path in a configuration file.
Add -> New Item -> Application Configuration File
Then you can use the following class to pull the value you're looking for:
System.Configuration.ConfigurationManager
Have a look here on the usage.
If the images you are referring to are non-changing and are simply acting as a resource for your application then you could consider embedding them in resource files which are compiled as part of your assembly. That way you need never worry about paths or copying them.
If the BMP file is used in the code and is accessed in runtime, there is no point in keeping it just in the project directory. it should be also present in the outputdirectory.
i.e you could write a post-build command to copy your folder to the output directory.
like copy "$(ProjectDir)\MyFolder\*.*" "$(OutDir)"
then in the code you could just write the relative path i.e "MyFolder\MyPoints.bmp"
But please remember one thing. If this BMP file is not going to change through out your program execution, it is better that you put it as a resource.
You can use something like GetCurrentPath(), which will return your .exe file path, then add to this path your \myfolder\mypoints.bmp. This will not be depend on C or D or ... drive and folder
There is one path that you can always reliably find, no matter how your program got deployed to the target machine: the directory in which your .exe is stored.
Take advantage of this, put the .bmp file in the same directory. Or a subdirectory of the install directory. You didn't say what kind of program you wrote, it is easy with Application.ExecutablePath in Windows Forms. But the generic solution that works everywhere:
public static string GetImagePath(string filename) {
string exeFile = System.Reflection.Assembly.GetEntryAssembly().Location;
string exePath = System.IO.Path.GetDirectoryName(exeFile);
string imgPath = System.IO.Path.Combine(exePath, #"images");
string imgFile = System.IO.Path.Combine(imgPath, filename);
return imgFile;
}
Which assumes the images are stored in a subdirectory named "images". Tweak as necessary.
Last but not least: don't forget that you can add images to your program's resources so it is baked in the .exe file. Project + Properties, Resources tab. Highly recommended.
You can also use predefined or existed system variable. Anyway you must decide when the user (or you) have to define that path (in config file or wherever) - during instalaltion, first run, before first run or in any time when app is running.
Personally I would use the Environment class in C#. Then I will locate the local app settings folder using the Environment.SpecialFolder enum.
I will then store all my applications specific files inside a folder in the local app settings folder.
This way you are also UAC safe, since UAC will complain if you want to store files in Program Files if you are not Administrator.
In my c# window application. I have 2 folders in the same path where exe is exist. One is Input folder where we put all files for process and second is processed folder where after processing files are moved.
Let say we have file name 1.txt at input folder after processing the file is cut from input and moved into processed folder. Immediatly when i placed same name file 1.txt at input folder then after processing it will again moved into processed folder where already another file exist of same name 1.txt.
Her i found that old 1.txt automatically moved out from processed folder and comes in the path where exe is placed. and new 1.txt will comes in processed folder.
I would like to know whether dotnet behaves in same way or i did something wrong?
It is not part of .NET (or the underlying Win32) to move files when they are overwritten. So, yes it is something you are doing.
Probably best to explicitly specify the path.
Also beware of using System.Environment.CurrentDirectory, because that is the systemwide current directory. Another program (like Windows Explorer, or some other program's file open/save dialog) can change that.
If it's a WinForms app you can use Application.StartupPath. If not I think you can use
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().Location)
View > Solution Explorer > Right Click on 1.txt > Properties > Copy to output directory
NOTE:Your problem has got nothing to do with .NET.