silhouette hustler
silhouette hustler

Reputation: 1763

How to create a folder and file inside of the folder where the app is installed? c#

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?

Upvotes: 1

Views: 1011

Answers (4)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

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

Upvotes: 2

Alex Mazzariol
Alex Mazzariol

Reputation: 2516

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.

Upvotes: 3

Casey
Casey

Reputation: 693

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");
    }
}

Upvotes: 0

Roland Mai
Roland Mai

Reputation: 31077

This applies to both web and windows apps:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

Upvotes: 2

Related Questions