.NET: C#: 擷取當前路徑

來源:互聯網
上載者:User

標籤:

 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 ConsoleTest13 {14     public class Program15     {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             else36                 Console.WriteLine("{0} Directory does not exist", path);37             if (File.Exists(path))38                 Console.WriteLine("{0} File exists", path);39             else40                 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             else45                 Console.WriteLine("{0} Directory does not exist", path);46             if (File.Exists(path))47                 Console.WriteLine("{0} File exists", path);48             else49                 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#: 擷取當前路徑

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.