|
-
Aug 23rd, 2011, 04:58 PM
#1
Thread Starter
Frenzied Member
[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.
-
Aug 23rd, 2011, 06:43 PM
#2
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"));
-
Aug 24th, 2011, 04:28 PM
#3
Re: how come it's not writing the the executable's directory?
 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.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Aug 24th, 2011, 04:43 PM
#4
Hyperactive Member
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?
-
Aug 24th, 2011, 07:35 PM
#5
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.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Aug 26th, 2011, 03:28 PM
#6
Thread Starter
Frenzied Member
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.
-
Aug 26th, 2011, 08:02 PM
#7
Re: how come it's not writing the the executable's directory?
 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?
 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.

Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Aug 28th, 2011, 06:54 PM
#8
Re: how come it's not writing the the executable's directory?
 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.
-
Aug 31st, 2011, 01:52 AM
#9
Thread Starter
Frenzied Member
Re: how come it's not writing the the executable's directory?
 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)
-
Aug 31st, 2011, 07:56 AM
#10
PowerPoster
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?
-
Aug 31st, 2011, 06:29 PM
#11
Re: how come it's not writing the the executable's directory?
 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
-
Sep 6th, 2011, 12:28 PM
#12
Thread Starter
Frenzied Member
Re: how come it's not writing the the executable's directory?
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|