Some time ago, I needed to know the pathname of my running application, in order to create a temporary file.
As it seems ther are alot more options than I thought.
So I applied all I could find in this little snippet.
I also used the full names to make clear where everything is coming from.
Guess I'll stick to System.Environment.CurrentDirectory;
:)
Let me know if you know others.
Enjoy!
Getting path name of working directory
using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
namespace DirectoryTest
{
class Program
{
static void Main(string[] args)
{
List<string> DirNames = new List<string>();
// To get the directory name with the exe file:
//"C:\\Users\\. . .\\Projects\\DirectoryTest\\bin\\Debug\\DirectoryTest.exe"
DirNames.Add("-------------- + exe");
DirNames.Add(System.Reflection.Assembly.GetExecutingAssembly().Location); //exe shows as exe
DirNames.Add(System.Reflection.Assembly.GetExecutingAssembly().CodeBase); //exe shows as EXE
DirNames.Add(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //exe shows as EXE
DirNames.Add(System.Reflection.Assembly.GetEntryAssembly().Location);
DirNames.Add(new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath);
// Give directory names (working directory of executable):
//"C:\\Users\\. . .\\Projects\\DirectoryTest\\bin\\Debug\\"
DirNames.Add("-------------- WD");
DirNames.Add(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
DirNames.Add(new System.IO.FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName);
DirNames.Add(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
DirNames.Add(System.IO.Directory.GetCurrentDirectory());
DirNames.Add(System.Environment.CurrentDirectory);
DirNames.Add(System.AppDomain.CurrentDomain.BaseDirectory);
//string appl = System.Windows.Forms.Application.StartupPath;
System.IO.DirectoryInfo di2 = new DirectoryInfo("DirectoryTest.exe");
DirNames.Add(di2.FullName);
DirNames.Add("-------------- process");
// To get the directory with name of process file:
//"C:\\Users\\. . .\\Projects\\DirectoryTest\\bin\\Debug\\DirectoryTest.vshost.exe"
DirNames.Add(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
DirNames.Add(Environment.GetCommandLineArgs()[0]);
foreach (var item in DirNames)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.