Correct File Path within C# Console Application
You can also add data.xml to your output build directory. You will need to right click on the file and select Copy if newer
or Copy always
for the Copy to output directory option
.
Then you can use the following:
Console.WriteLine(File.Exists("App_Data/data.xml")); // Should print True
However, if you expect data.xml to change then just point to the full path, somewhere outside your project (assuming App_Data/data.xml is copied to C:/Temp) then:
Console.WriteLine(File.Exists("C:/Temp/App_Data/data.xml")); // Prints True
Also, for your other question related to a console and an executable application, the exe can be a different application type such as console or wpf or winforms (that's the way visual studio groups these).
Get the current working directory:
Directory.GetCurrentDirectory() + "/App_Data/data.xml";
Important: App_Data
has to be in the same directory as your application is.
The best way to get your data.xml
file is to carry it with your application or to save it e.g. in the "home" directory of your user (or whatever it is called under Windows 8).