[RESOLVED] how come it's not writing the the executable's directory?
I have a windows forms application, and it uses a class (which I made). Inside that class, I have this code:
StreamWriter sw = new StreamWriter("a.txt");
Currently, it is writing to the C:\ drive. Is there a way for me to make it write into my executable's folder without me having to changing it to:
StreamWriter sw = new StreamWriter(execPath + @"\a.txt");
where execPath is the path of the executable.
Re: how come it's not writing the the executable's directory?
How are you running your program? A relative filepath goes from the current working directory, which must be C:\ in your case. This is controlled from the environment where the application was started.
Writing to the application's directory is not recommended for a Windows application that is to be installed, however. Consider using Environment.SpecialFolder.ApplicationData or Environment.SpecialFolder.CommonApplicationData (for specific user data/all user data respectively) as your base folder.
Also, when concatenating paths, it is better to use the classes supplied for doing so:
csharp Code:
StreamWriter sw = new StreamWriter(Path.Combine(execPath, "a.txt"));
Re: how come it's not writing the the executable's directory?
Quote:
Originally Posted by
benmartin101
I have a windows forms application, and it uses a class (which I made). Inside that class, I have this code:
StreamWriter sw = new StreamWriter("a.txt");
Currently, it is writing to the C:\ drive. Is there a way for me to make it write into my executable's folder without me having to changing it to:
StreamWriter sw = new StreamWriter(execPath + @"\a.txt");
where execPath is the path of the executable.
Yes. You can have Environment.CurrentDirectory().
As EvilGiraffe pointed out, it is not a wise idea to write to the application folder.
Re: how come it's not writing the the executable's directory?
AFAIK environment.currentdirectory is not always going to be the application folder. You would want application.startuppath. What is the reason behind it not being wise to write to the app folder?
Re: how come it's not writing the the executable's directory?
I have one example. You do not know where the application is installed. The user running the code might not have administrative access on his machine. So any attempt to write to c:\PRogram Files\YourApp might result in a runtime failure. That's one reason.
Re: how come it's not writing the the executable's directory?
Thanks for the help and tips. However, assuming the users will have access rights to the application folder, is there a way to do it as shown in my first post? I would like to know if it's possible.
Re: how come it's not writing the the executable's directory?
Quote:
Originally Posted by
Grunt
AFAIK environment.currentdirectory is not always going to be the application folder. You would want application.startuppath. What is the reason behind it not being wise to write to the app folder?
Quote:
Originally Posted by
benmartin101
Thanks for the help and tips. However, assuming the users will have access rights to the application folder, is there a way to do it as shown in my first post? I would like to know if it's possible.
:afrog::thumb:
Re: how come it's not writing the the executable's directory?
Quote:
Originally Posted by
benmartin101
Thanks for the help and tips. However, assuming the users will have access rights to the application folder, is there a way to do it as shown in my first post? I would like to know if it's possible.
If you mean without changing these lines to include a full path:
csharp Code:
StreamWriter sw = new StreamWriter("a.txt");
That will always write to the current working directory. I cannot stress enough that to change the working directory to the application's folder is not the behaviour expected of a Windows program. It is possible to do, but for the above mentioned reasons I am not going to signpost the way for you.
Re: how come it's not writing the the executable's directory?
Quote:
Originally Posted by
Evil_Giraffe
If you mean without changing these lines to include a full path:
csharp Code:
StreamWriter sw = new StreamWriter("a.txt");
That will always write to the current working directory. I cannot stress enough that to change the working directory to the application's folder is not the behaviour expected of a Windows program. It is possible to do, but for the above mentioned reasons I am not going to signpost the way for you.
So the proper programming "etiquette" is to NOT put any output files (created by the program) in the executable's folder/subfolder? (Assuming the user is not given any choice to which path to save it to)
Re: how come it's not writing the the executable's directory?
1) what is it you are trying to do exactly? What is its purpose and goal?
2) why are you not able to use Application.StartupPath to write to the app folder from where the app is executing from?
Re: how come it's not writing the the executable's directory?
Quote:
Originally Posted by
benmartin101
So the proper programming "etiquette" is to NOT put any output files (created by the program) in the executable's folder/subfolder? (Assuming the user is not given any choice to which path to save it to)
Exactly. Use the SpecialFolders enumeration provided by .NET to get to the folder Windows wants you to use. Exactly which one you should use depends on what sort of file you're saving.
See:
http://msdn.microsoft.com/en-us/libr...ialfolder.aspx
and http://msdn.microsoft.com/en-us/library/dd783871.aspx
Re: how come it's not writing the the executable's directory?
Quote:
Originally Posted by
Techno
1) what is it you are trying to do exactly? What is its purpose and goal?
2) why are you not able to use Application.StartupPath to write to the app folder from where the app is executing from?
1) Just wanting to know if it's possible
2) I am able if I really need to, but I wanted to know if it's possible to do it without using Applicaton.StartupPath.