ASP.NET/C# 獲取當前目錄、當前完整路徑、文件全路徑、目錄、擴展名、文件名稱


using System;
using System.IO;

namespace ConsoleApp5
{
class Program
{
/*
C# 獲取當前目錄、當前路徑、文件全路徑、目錄、擴展名、文件名稱
*/
static void Main(string[] args)
{
//1.獲取模塊的完整路徑。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

//2.獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄
string path2 = System.Environment.CurrentDirectory;

//3.獲取應用程序的當前工作目錄
string path3 = System.IO.Directory.GetCurrentDirectory();

//4.獲取程序的基目錄
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;

//5.獲取和設置包括該應用程序的目錄的名稱
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

//6.獲取啟動了應用程序的可執行文件的路徑
//string path6 = System.Windows.Forms.Application.StartupPath;

//7.獲取啟動了應用程序的可執行文件的路徑及文件名
//string path7 = System.Windows.Forms.Application.ExecutablePath;
Console.WriteLine(path1);
Console.WriteLine(path2);
Console.WriteLine(path3);
Console.WriteLine(path4);
Console.WriteLine(path5);

//獲取當前運行程序的目錄
string fileDir = Environment.CurrentDirectory;
Console.WriteLine("當前程序目錄:" + fileDir);

//一個文件目錄
string filePath = "C:\\123.jpg";
Console.WriteLine("該文件的目錄:" + filePath);

string str = "獲取文件的全路徑:" + Path.GetFullPath(filePath);
Console.WriteLine(str);
str = "獲取文件所在的目錄:" + Path.GetDirectoryName(filePath);
Console.WriteLine(str);
str = "獲取文件的名稱含有后綴:" + Path.GetFileName(filePath);
Console.WriteLine(str);
str = "獲取文件的名稱沒有后綴:" + Path.GetFileNameWithoutExtension(filePath);
Console.WriteLine(str);
str = "獲取路徑的后綴擴展名稱:" + Path.GetExtension(filePath);
Console.WriteLine(str);
str = "獲取路徑的根目錄:" + Path.GetPathRoot(filePath);
Console.WriteLine(str);
Console.ReadKey();
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM