码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-07-20 18:40:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

 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

 

技术分享

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: C#: 获取当前路径

标签:

原文地址:http://www.cnblogs.com/yingzhongwen/p/4661830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!