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?
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?
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);
}
}
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.
If you want the program execution path:
System.Environment.CurrentDirectory
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.