I have a program that can edit a certain text file. Currently, I am only running it in Visual Studios. When I am referring to the text file, the file path looks something like this:
#"C:\myProjectFolder\someTextFile.txt"
For now, I can use this path and it works flawlessly, but after I deploy it, the program would only work on my computer because this path is specific to my computer.
In HTML for example, if I was linking a CSS file, it would be possible to do \stylesheets\style.css instead of C:\myWebsite\stylesheets\style.css
How can I achieve something similar in C#
Use System.Windows.Forms.Application.StartupPath and Application.executablePathfor getting current application path.
if file is in current directory of .Exe file full path is not required and just name of file is enough. you also can point to current path in some situations by ".\\"
Related
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 write a code that upload file and i need the path of file.
i write something like that:
private string path = System.IO.Path.Combine(AppDomain.CurrentDomain.
BaseDirectory, #"folder1\Files\", "file.jpg");
return me this path:
C:\Users\name\source\repos\projectFile\bin\Debug\folder1\Files
Is it possible to write this line in another way maybe shorter:
System.IO.Path.Combine(AppDomain.CurrentDomain. BaseDirectory, #"folder1\Files\", "file.jpg");
the main purpose of this is that i want to run this code from server
and the path can be change according to each server.
Note: I marked in visual studio in the settings of the file that I want to upload copy of this file to the debug folder.
thanks!
I'm designing a script generator using winforms. Scope is to generate few update/Insert queries. I've template of update/insert queries within my project in a folder in format of .text.
text = File.ReadAllText(#"\Visual Studio 2013\Projects\MigrationScript\MigrationScript\Scripts\Schema_OWNER.SYS_PARAMS.txt");
text = text.Replace(Constants.LOWER_VER, lowerversion)
.Replace(Constants.CURRENT_VER, currentversion);
System.IO.Directory.CreateDirectory(string.Format(Constants.DIRECTORY_CCB_SEED_OWNER, releaseVal));
File.WriteAllText(string.Format(Constants.DIRECTORY_CCBOWNER_SYS_PARAMS, releaseVal), text);
It works like charm in my machine. But when i extract the .exe and run in another machine, i'm getting error like System.IO.DirectoryNotFoundException: Could not find a part of the path
How to include external files within the project into my .exe, so that i could run in any machine?? Believe i explained my issue. If not please revert me.
System.IO.DirectoryNotFoundException: Could not find a part of the path clearly states that path is not accessible from the system where your .exe is placed and being run. Make Sure Whatever path you have given should be accessible from the System's where your .exe is supposed to be executed.
I have a windows form program currently outputting to my desktop, It is a windows form that outputs a HTML. I understand I can change the Output directory using visual studio in the way described here
However I want to code in an output path that will override any Visual Studio settings. Something along the lines of...
outputPath = [path/string]
I've tried searching for this but all I can find is how to do it using visual studio.
I want to avoid using FolderBrowserDialog or SaveFileDialog as they promt the user to select a path, which is not what I want.
Guessing that you'd like to change the output directory of one of the things your code generates.
Perhaps this is what you are searching (Enviroment.SpecialFolder): https://msdn.microsoft.com/en-us/library/14tx8hby(v=vs.110).aspx
Implementation sample:
C# Get Special Folder
Or if you'd like a different path you can set it like this:
string path = "C:\\Example\\V1\\file.txt";
By default a program is writing files into its current working directory (if you do not set a path information while creating the file).
If you want to write to a different directory you can either set the filename with a path included (see answer by usselite).
Otherwise you can use the Directory.SetCurrentDirectory method to change you current working directory of the program.
Hope it helps.
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\");