I want to be able to set default folder and file creating inside of the folder where the app is installed? Because this app will be used on multiple machines so I cannot specify path like C://Users/PcName/etc..
Is there any very simple way of doing it?

- 31,361
- 18
- 86
- 116

- 1,733
- 7
- 30
- 56
-
1http://stackoverflow.com/questions/864484/getting-the-path-of-the-current-assembly Get path using the asessmbly location – Mahesh Malpani Aug 07 '15 at 08:29
-
What have you tried so far? Nobody will implement your application. But we're looking forward helping you :). – SSchuette Aug 07 '15 at 08:30
-
I have just had my path including my pc name since I was building it. But now I have to change it , and I was just wandering if there some very simple way of doing it , Candide gave me the solution for my problem , I just need to wait 5 mins more to mark it as a correct answer :)) – threesixnine Aug 07 '15 at 08:36
-
Do you mean location of `.exe`? – Hossein Narimani Rad Aug 07 '15 at 08:36
-
@HosseinNarimaniRad I do but also is there any sort of method searching for User name ? Therefore I can set the folder to be created on Desktop. – threesixnine Aug 07 '15 at 08:38
-
@Mystia That would be a different question and you may post another question for that but [this](http://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c) may helps you – Hossein Narimani Rad Aug 07 '15 at 08:40
-
1You can get the desktop for the current user from Environment.GetFolderPath(Environment.SpecialFolders.Desktop). Do not try to build the path, it may not be where you expect it to be, ask the system. – Alex Mazzariol Aug 07 '15 at 08:43
-
1@Mystia I advise you to test the Alex's approach and mark it as the correct answer because it seems that's the better solution. – Hossein Narimani Rad Aug 07 '15 at 08:45
4 Answers
What you are trying to do is not advisable; if your application is installed using recommended default methods (following Microsoft guidelines) the app will be in a directory under C:\Program Files
(or where the program files folder may be redirected) and the user that runs the app will not have write access to that directory, so the directory creation will fail.
That said, you cannot use the Environment.CurrentDirectory
, because it may or may not be the directory where your application's executable files reside, neither CurrentDomain.BaseDirectory
, because that is not significative too (documentation says it's the directory where the loader will search for assemblies, but that may or may not be the directory of your application's executable files).
Copying from this other answer, the correct way to find the directory of your assembly is
public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Once you have the path, you can try to create the directory with System.IO.Directory.CreateDirectory()
and a file with System.IO.File.WriteAllText()
or its siblings, or any other standard method of creating files.
You may also want to use the newer Assembly.GetExecutingAssembly().Location
property, and use Path.GetDirectoryName()
on that.

- 1
- 1

- 2,456
- 1
- 19
- 21
-
Seems it's the better approach however i didn't try it yet. – Hossein Narimani Rad Aug 07 '15 at 08:46
You can get the path for the executable using this code (most of the time, actually it returns the current working directory)
System.Environment.CurrentDirectory

- 31,361
- 18
- 86
- 116
-
1Wrong, that is the current directory the application was started from. It may or may not be the directory where the app is installed. – Alex Mazzariol Aug 07 '15 at 08:32
-
1@AlexMazzariol as i mentioned in the answer it's the location of executable file (so not necessarily the installation directory) and seems this is what he means by installation directory. – Hossein Narimani Rad Aug 07 '15 at 08:35
-
1It's not the location of the executable, it's where it was started from. Try from console, in directory X, to start the program in directory Y giving the full path: the result will be the the X directory, not Y where the program resides. – Alex Mazzariol Aug 07 '15 at 08:41
This applies to both web and windows apps:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

- 30,469
- 8
- 53
- 60
-
There are ways to start the AppDomain with a custom BaseDirectory, so it may not be the location of the executing assembly (where the app is supposed to be located). – Alex Mazzariol Aug 07 '15 at 08:43
-
1Yes, sure, however, I doubt the person asking this question has to deal with such an advanced scenario. Actually, not trying to be mean and poke at your answer, however, you can technically load a dll dynamically which maybe located somewhere else, and the executing assembly may not be in the same place as the "app". Anyways, I doubt the person asking the question here is facing any such issues. – Candide Aug 07 '15 at 09:04
-
Yes, if the code was in a DLL it may be somewhere else, and yes, that's an advanced scenario - that's because there is no formal definition for "the folder where the app is installed". That "easy scenario mode" is also why I added a recommendation not to do that, because correctly installed programs are in read-only folders for everybody except TrustedInstaller, for security purposes. – Alex Mazzariol Aug 07 '15 at 09:13
If you only specify a path that doesn't start with a drive letter then the path will be relative to where the application is running. e.g. The following program will create a folder and file in the application's local folder.
class Program
{
static void Main(string[] args)
{
var installedLocation = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
var di = installedLocation.CreateSubdirectory("MyFolder");
File.WriteAllText(Path.Combine(di.FullName, "File.txt"), "This will be written to the file");
var installedPath = AppDomain.CurrentDomain.BaseDirectory;
var di2 = Directory.CreateDirectory(Path.Combine(installedPath, "MyFolder2"));
File.WriteAllText(Path.Combine(di.FullName, "File2.txt"), "This will be written to the file");
}
}

- 693
- 7
- 18