.NET: C#: 获取当前路径

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Xml.Linq;

 6 using System.Xml;

 7 using System.Configuration;

 8 using System.Reflection;

 9 using System.Windows.Forms;

10 using System.IO;

11 

12 namespace ConsoleTest

13 {

14     public class Program

15     {

16         static void Main(string[] args)

17         {

18             int a = 0;

19             Console.WriteLine(Assembly.GetExecutingAssembly().Location);

20             Console.WriteLine(a.GetType().Assembly.Location);

21             Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

22             Console.WriteLine(System.Environment.CurrentDirectory);

23             Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);

24             Console.WriteLine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);

25             Console.WriteLine(System.Windows.Forms.Application.StartupPath);

26             Console.WriteLine(System.Windows.Forms.Application.ExecutablePath);

27             Console.WriteLine(System.IO.Directory.GetCurrentDirectory());

28             Console.WriteLine();

29             string path = "d:asdfasdf.bmp";

30             Console.WriteLine(Path.GetFileName(path));

31             Console.WriteLine(Path.GetExtension(path));

32             path = @"C:\Users\Administrator\Desktop\Demo";

33             if (Directory.Exists(path))

34                 Console.WriteLine("{0} Directory exists", path);

35             else

36                 Console.WriteLine("{0} Directory does not exist", path);

37             if (File.Exists(path))

38                 Console.WriteLine("{0} File exists", path);

39             else

40                 Console.WriteLine("{0} File does not exist", path);

41             path = @"C:\Users\Administrator\Desktop\Demo\Book.XML";

42             if (Directory.Exists(path))

43                 Console.WriteLine("{0} Directory exists", path);

44             else

45                 Console.WriteLine("{0} Directory does not exist", path);

46             if (File.Exists(path))

47                 Console.WriteLine("{0} File exists", path);

48             else

49                 Console.WriteLine("{0} File does not exist", path);

50             Console.WriteLine();

51             Console.WriteLine(Path.GetDirectoryName(path));

52             Console.WriteLine(Path.GetExtension(path));

53             Console.WriteLine(Path.GetFileName(path));

54             Console.WriteLine(Path.GetFileNameWithoutExtension(path));

55             Console.WriteLine(Path.GetPathRoot(path));

56             Console.WriteLine(Environment.SystemDirectory);

57         }

58     }

59 }

result

 

.NET: C#: 获取当前路径

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

 

你可能感兴趣的:(.net)