darnir
darnir

Reputation: 5190

C# File Handling: Create file in directory where executable exists

I am creating a standalone application that will be distributed to many users. Now each may place the executable in different places on their machines.
I wish to create a new file in the directory from where the executable was executed. So, if the user has his executable in :

C:\exefile\

The file is created there, however if the user stores the executable in:

C:\Users\%Username%\files\

the new file should be created there.

I do not wish to hard code the path in my application, but identify where the executable exists and create the file in that folder. How can I achieve this?

Upvotes: 10

Views: 50710

Answers (6)

Saeed Amiri
Saeed Amiri

Reputation: 22565

Just use File.Create:

File.Create("fileName");

This will create file inside your executable program without specifying the full path.

Don't forget to add:

using System.IO;

Upvotes: 13

Chris Brooks
Chris Brooks

Reputation: 1

I like to give the user the choice. I would default the directory to something like Environment.SpecialFolder.CommonApplicationData and let them read and edit the path at will. If in a console app display the path in help and allow them to pass it via command line argument. This saves them the hassle of hunting for the folder. If they point to a path you cannot write to then you throw the error and let them decide what to do.

Upvotes: 0

Ryan Naccarato
Ryan Naccarato

Reputation: 1211

In modern operating systems, the accepted answer of:

var systemPath = System.Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData
);
var complete = Path.Combine(systemPath , "files");

will produce a user agnostic path like: "C:\ProgramData\files"

To produce a user-based path similar to: "C:\Documents and Settings\%USER NAME%\Application Data\files"

You should use SpecialFolder.ApplicationData or SpecialFolder.LocalApplicationData instead.

Upvotes: 2

Steve Smith
Steve Smith

Reputation: 2270

You can get the full path to your new file with:

string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\mynewfile.txt" 

Upvotes: 8

Tigran
Tigran

Reputation: 62265

Never create a file into the directory where executable stays. Especially with the latest OSes available on the market, you can easily jump into the security issues, on file creation. In order to gurantee the file creation process, so your data persistancy too, use this code:

var systemPath = System.Environment.
                             GetFolderPath(
                                 Environment.SpecialFolder.CommonApplicationData
                             );
var complete = Path.Combine(systemPath , "files");

This will generate a path like C:\Documents and Settings\%USER NAME%\Application Data\files folder, where you guaranteed to have a permission to write.

Upvotes: 30

bizl
bizl

Reputation: 1563

string path;
   path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
   MessageBox.Show( path );

http://msdn.microsoft.com/en-us/library/aa457089.aspx

Upvotes: 2

Related Questions