C# creates file with name <Debug><GivenName> - c#

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.

Related

Getting the path of a file from the project

My structure is like this picture below:
How can I write a code that gets me the path to the highlighted one which is "ConenctionsList.xml" ?
I tried something like this but this take me to bin folder
this.XmlPlath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"ConnectionsList.XML");
It is good that XmlPath points to the bin folder, the trick is to set ConnectionList.xml to be copied in the output. Just, enter its properties and set Copy to Output to Always or even better - If Newer.
Note: Imagine that your application will be deployed and your paths within the project or solution do not make sense in the deployed location. Only relative paths to the executable (or some assembly) are important (except if GAC registering or something similar is used).
You need to know what the absolute path of that file is to reference it in code. Referencing it by where it resides in Visual Studio is not adequate. More often than not, if you are running a program and wish to read in a config file of some sort, the designer will put the configuration file in the same place as the executable and reference it using various methods, one of which is listed below:
string filename = "ConnectionsList.xml";
string filePath = Path.Combine(Directory.GetCurrentDirectory(),filename);
Please reference links below, and other threads on Stack Overflow for any additional details. This general topic is addressed in numerous places.
MSDN GetCurrentDirectory
MSDN Path.Combine

How do I set the output directory in c# using code?

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.

System.IO.Path.Combine(Environment.CurrentDirectory,"") takes me to wrong path

I am loading this file which is located in a folder i created, the same folder is also rendered in the bin/debug folder, but i need to locate the file in the folder i created, the thing is the method which should find the path of the file by its name gives me the path of the debug folder instead of the on i created.
I have harcoded the path to show you the real location.
var path2 = System.IO.Path.Combine(Environment.CurrentDirectory, "xulrunner");
var hardcodedPath= "C:\\Users\\Alan\\Desktop\\TabControl\\TabControl\\TabControl\\TabControl\\xulrunner";
This is the result of path2 variable:
C:\\Users\\Alan\\Desktop\\TabControl\\TabControl\\TabControl\\TabControl\\bin\\Debug\\xulrunner"
As you can see its pointing to different folder as hardcoded path.
When running from Visual Studio, the working directory is different from your project location, usually it is two folders up (from bin\Debug\). In your distributed application, the working directory might be the same as that of other files you want to access since you copy some other files to the working directory.
I would recommend to keep your code the same, and copy the files you require from your project to the output directory. You can do that by simply setting the Copy to Output Directory setting to Copy always or Copy if newer.
Changing the path to go two folders up is a bad idea, since this won't be true in production scenarios, and will thus break your code.
To get exactly what you are looking for, you could do the following:
var path2 = System.IO.Path.GetFullPath(System.IO.Path.Combine(Environment.CurrentDirectory, "..\\..\\xulrunner"));
However, please be aware that Environment.CurrentDirectory can change as your program runs. I have found that using the %PROGRAMDATA% environment variable works well for creating a location where you save data. It allows for a more consistent solution for using paths.
Additionally, the code snippet provided will only really be useful when you are running through the IDE's debugger. When you deploy, it will likely not give you what you are expecting.
It would help to know what is the overall functional result you are hoping for.

avoid hard coding path in ReadAllText

My exact file path is as follows. This .txt file is not supposed to be deployed to bin/debug
string str = File.ReadAllText(#"C:\development\slnfolder\projfolder\myfile.txt");
How can I write the code so that I do not have to hard code full path to get to the file
I am trying to avoid hard coding path in the above line of code as follows:
string file = #"myfile.txt";
string str = Path.GetFullPath(file);
but the str ends up being as follows and is not able to find the file.
C:\development\slnfolder\projfolder\bin\debug\myfile.txt
You can include myfile.txt in your Visual Studio solution and go to its properties and set the Build action to Copy always (or Copy if newer if you want to avoid copying the file if it didn't change since the previous build...).
This way you're going to have the whole file in the target directory (i.e. bin\debug).
That's where it should map, because that's where your executable is running from. I'd highly suggest you ensure that the text file is moved to the bin/debug folder (there's a VS option to copy it down in properties) rather than trying to read two levels up. It will be much easier once you end up deploying your app instead of running it from visual studio.
If you're using Visual studio, than add the txt file to your project
right click on properties
set build action to none
and set copy to output directory to copy if newer
this will ensure that the txt file is always in the same folder as your executable
To avoid hard-coding something, you should:
"Soft-code" it (i.e. make it part of your product's configuration). You can use Configuration Settings APIs for that.
Take it as a parameter on the command line (read the directory location from one of the args passed to the Main method), or
Make a convention as to where it should be located, for example, in the data directory, which is a subdirectory of your current running directory (read from #"..\data\myfile.txt").
You can always define a combination of these methods, for example, use the "by convention" location when the configuration / command line option has not been specified.

XNA: Load from subfolder in Content

I'm running into a lot of trouble while attempting to streamline XNA texture loading. I've given up on dynamically loading textures from a specified directory, so now I'm just trying to work with an organized collection of folders in the solution's content. I'm trying to use the following line of code:
Content.Load<Texture2D>(".\\Graphics\\Characters\\Char1");
"Char1" is the correct asset name, and it is stored in "Graphics\Characters" under Content in my solution explorer, but it still throws a "file not found" error. What am I doing wrong here?
EDIT: Turns out the debug bin folder created the wrong directory stucture: \Graphics\Characters\Characters. Thanks for the help regardless!
Try removing your leading slash. eg,
Content.Load<Texture2D>("Graphics\\Characters\\Char1");
Are you using a content project for these assets? You can set the 'Content Root' directory and the assets are all in relative paths from there.
Have a look at this msdn entry, hopefully that might help you out. Good luck.
From what you're saying, that the file is located in 'Content', make sure you do this before loading up anything:
Content.RootDirectory = "Content"
And get rid of the '.\' part.
You can set the RootDirectory property in Content to the root location of your content files. This will make your paths work correctly.
Also, no need for the single dot .\
What is your working directory? Typically it is bin\Debug (the location of the exe that is generated) and unless you are telling Visual Studio to copy your content files to this directory they are not located there.
Several options to fix this
You can add your content files to Visual Studio and set them to Deploy mode of Copy
You can manually copy them once
You can check to see if the current directory contains a particular file and go up two levels if it does not
You can use a constant to define the base directory and flex it depending on whether you are debugging or not.
Here is an example of 3
private string BaseDirectory = ".";
if (!Directory.Exists(".\Graphics"))
{
BaseDirectory = #"..\..";
}
...
Content.Load<Texture2D>(BaseDirectory + #"\Graphics\Characters\Char1");
Here is an example of the 4, note that if you are running in release mode this won't work too smoothly.
#if DEBUG
private const string BaseDirectory = #"..\..";
#else
private const string BaseDirectory = #".";
#endif
...
Content.Load<Texture2D>(BaseDirectory + #"\Graphics\Characters\Char1");
Also note that a string that starts with # does not allow \ escapes and is thus much easier to use to define directories.

Categories