using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using System.Xml; using System.Configuration; using System.Reflection; using System.Windows.Forms; using System.IO; namespace ConsoleTest { public class Program { static void Main(string[] args) { ; Console.WriteLine(Assembly.GetExecutingAssembly().Location); Console.WriteLine(a.GetType().Assembly.Location); Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); Console.WriteLine(System.Environment.CurrentDirectory); Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory); Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase); Console.WriteLine(System.Windows.Forms.Application.StartupPath); Console.WriteLine(System.Windows.Forms.Application.ExecutablePath); Console.WriteLine(System.IO.Directory.GetCurrentDirectory()); Console.WriteLine(); string path = "d:asdfasdf.bmp"; Console.WriteLine(Path.GetFileName(path)); Console.WriteLine(Path.GetExtension(path)); path = @"C:\Users\Administrator\Desktop\Demo"; if (Directory.Exists(path)) Console.WriteLine("{0} Directory exists", path); else Console.WriteLine("{0} Directory does not exist", path); if (File.Exists(path)) Console.WriteLine("{0} File exists", path); else Console.WriteLine("{0} File does not exist", path); path = @"C:\Users\Administrator\Desktop\Demo\Book.XML"; if (Directory.Exists(path)) Console.WriteLine("{0} Directory exists", path); else Console.WriteLine("{0} Directory does not exist", path); if (File.Exists(path)) Console.WriteLine("{0} File exists", path); else Console.WriteLine("{0} File does not exist", path); Console.WriteLine(); Console.WriteLine(Path.GetDirectoryName(path)); Console.WriteLine(Path.GetExtension(path)); Console.WriteLine(Path.GetFileName(path)); Console.WriteLine(Path.GetFileNameWithoutExtension(path)); Console.WriteLine(Path.GetPathRoot(path)); Console.WriteLine(Environment.SystemDirectory); } } }
result
we usually use codes as below to get current directory:
System.Environment.CurrentDirectory
System.Windows.Forms.Application.StartupPath(using System.Windows.Forms;)
System.IO.Directory.GetCurrentDirectory() (using System.IO;)
I prefer: Enviroment.CurrentDirectory