How to get the executable path open in C #

5

How do I get the project path in C #? Is there a function?
Or do I have to type the full path in my application?

    
asked by anonymous 12.02.2014 / 19:02

3 answers

8

Use:

String path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();

To create a sub folder you can do this:

class Program
{
    static void Main(string[] args)
    {

        string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
        //
        string path2 = path.Remove(path.LastIndexOf("\"));
        path = path2.Remove(path2.LastIndexOf("\") + 1);
        path += "log";

        System.IO.Directory.CreateDirectory(path);

    }
}

EDIT

Reviewing a code I found another implementation that does the same thing:

string curDir = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory.ToString());

Then just put the string with the path you want to create. If it is on the same level, do the following:

string new_dir = curDir + "\..\log";

System.IO.Directory.CreateDirectory (new_dir);

Do not forget to make all the consistencies.

    
12.02.2014 / 19:07
2

If you want the program execution path:

System.Environment.CurrentDirectory
    
18.02.2014 / 17:13
1

The method reported by @Isalamon is correct. But you can also search for the assembly:

String currentPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Models.Configuracao)).Location);

Where Models.Configuration is just replace by the name of the assembly that you want to find the path to.

    
12.02.2014 / 19:14