Results 1 to 12 of 12

Thread: [RESOLVED] how come it's not writing the the executable's directory?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Resolved [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.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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:
    1. StreamWriter sw = new StreamWriter(Path.Combine(execPath, "a.txt"));

  3. #3
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: how come it's not writing the the executable's directory?

    Quote Originally Posted by benmartin101 View Post
    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

  4. #4
    Hyperactive Member Grunt's Avatar
    Join Date
    Oct 2004
    Location
    Las Vegas
    Posts
    499

    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?
    My Mobile Software Development Company - http://www.mbpsoftllc.com

  5. #5
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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.

  7. #7
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: how come it's not writing the the executable's directory?

    Quote Originally Posted by Grunt View Post
    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 View Post
    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

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: how come it's not writing the the executable's directory?

    Quote Originally Posted by benmartin101 View Post
    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:
    1. 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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: how come it's not writing the the executable's directory?

    Quote Originally Posted by Evil_Giraffe View Post
    If you mean without changing these lines to include a full path:

    csharp Code:
    1. 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)

  10. #10
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: how come it's not writing the the executable's directory?

    Quote Originally Posted by benmartin101 View Post
    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

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: how come it's not writing the the executable's directory?

    Quote Originally Posted by Techno View Post
    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
  •  



Click Here to Expand Forum to Full Width