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.
Related
I have a WPF desktop application. When I am in debug mode, I am able to access file.sql and read the contents.When I publish the file using visual studio and try to run the click once application,I am unable to read the file.sql file and the app cannot find the path. The .cs file where I am calling to read file.sql is located in the same folder.
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,#"..\..\ControlFolder","file.sql");
string readQuery = File.ReadAllText(path);
How can I ensure that I am able to read file.sql after publishing my app?
When you publish, you need to ensure that you are also publishing your "file.sql" to the binary output path. The best practice is to avoid using relative paths to your source code, and instead you should always make sure that you point to files that you (the developer) will ensure exists in the published area.
Here's what I would do:
In Visual Studio, go to the properties of "file.sql" and make sure it is set to "Content" and "Copy if newer". This will make sure that your "file.sql" will always exist in the binary output path. If in Visual Studio, you have placed "file.sql" inside of a folder called "ControlFolder", then that means your binary output contents will contain a ".\ControlFolder\file.sql" file.
Fix your code to never point to the relative path of your source code. Instead, rely on the binary output path. So instead of the code that you shared, replace it with this:
string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), #"ControlFolder\file.sql");
string readQuery = File.ReadAllText(path);
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 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 ".\\"
This may be a stupid question, but I've been googling it for about 15 minuts now.
Actually the word "Debug" is not in the common context, that's why google results won't help me.
I'm using Visual Studio 2012.
I have a simple C# code that's suppose to print some output to .post file:
string sCurrPath = Directory.GetCurrentDirectory()+"Posting_0";
TextWriter tw = new StreamWriter(sCurrPath + ".post");
tw.WriteLine("something");
tw.Close();
This is a part of a for loop, but I don't think it matters.
I'm expecting to find a file called
"Posting_0.post"
but what I get is a file called
"DebugPosting_0.post"
Maybe it's somewhere in the Visual Studio preferences.
When I try to look for answers in google, it misunderstands the context of "Debug".
Thanks in advance.
use Path.Combine(Directory.GetCurrentDirectory(), "Posting_0") instead of +.
your Directory.GetCurrentDirectory() would have Documents/Visual Studio 2012/projects/yourProject/Debug as the value, because you are currently running your project in the debug mode
so you might consider adding
/Posting_0 instead of Posting_0 to get the file with correct name inside the debug folder.
While debugging the current directory is Debug, so, you need to quit Debug from that path or put the file inside that folder for your program to work.
What's happening, as others have alluded to but not stated explicitly, is the string returned by GetCurrentDirectory() doesn't have a trailing slash. The executable is running from a subfolder of your project's folder called Debug (since you're in debug mode), E.G., <project folder>\Debug. This is the build output folder. You can change it to Release and you'll have a file with Release prepended in its name. As Daniel White said, using Path.Join() will work as it will insert the directory separator (\ on windows) and you'll wind up with your file named as expected in the debug folder.
As an aside, look at the DirectoryInfo class. You can pass its constructor the output of Directory.GetCurrentDirectory() then use that object's Parent.FullName property to get the directory to you exe's parent directory, E.g., your project directory.
EDIT: While it might seem ridiculous to have Path.Join() in the BCL since the path separator on windows is always \, it's there to remove the dependence/assumption that this is always the case. It's a good idea to just force yourself to use Path.Join() instead of \ because you never know what will happen to your code later on.
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.