winform相對路徑和絕對路徑

          絕對路徑:是從盤符開始的路徑,形如
          C:\windows\system32\cmd.exe
          相對路徑:是從當前路徑開始的路徑,假如當前路徑為C:\windows
          要描述上述路徑,只需輸入
          system32\cmd.exe
          實際上,嚴格的相對路徑寫法應為
          .\system32\cmd.exe
          其中,.表示當前路徑,在通道情況下可以省略,只有在特殊的情況下不能省略。
          假如當前路徑為c:\program files
          要調用上述命令,則需要輸入
          ..\windows\system32\cmd.exe
          其中,..為父目錄。
          當前路徑如果為c:\program files\common files
          則需要輸入
          ..\..\windows\system32\cmd.exe
          winform的默認相對路徑是相對于 bin文件夾下的debug文件夾的位置
          例如bin文件夾下的file文件夾的a.mp3文件 的相對路徑為 file/a.mp3
          bin文件夾外的 file文件夾的a.mp3文件 的相對路徑為 ../../file/a.mp3

          C# Winform中如何獲取文件路徑

          獲取文件名方法:

          用System.IO.Path.GetFileName和System.IO.Path.GetFileNameWithoutExtension(無擴展名)的方法

          獲取文件路徑方法:

          //獲取當前進程的完整路徑,包含文件名(進程名)。
          string str = this.GetType().Assembly.Location;
          result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

          //獲取新的 Process 組件并將其與當前活動的進程關聯的主模塊的完整路徑,包含文件名(進程名)。
          string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
          result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

          //獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。
          string str = System.Environment.CurrentDirectory;
          result: X:\xxx\xxx (.exe文件所在的目錄)

          //獲取當前 Thread 的當前應用程序域的基目錄,它由程序集沖突解決程序用來探測程序集。
          string str = System.AppDomain.CurrentDomain.BaseDirectory;
          result: X:\xxx\xxx\ (.exe文件所在的目錄+”\”)

          //獲取和設置包含該應用程序的目錄的名稱。
          string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
          result: X:\xxx\xxx\ (.exe文件所在的目錄+”\”)

          //獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。
          string str = System.Windows.Forms.Application.StartupPath;
          result: X:\xxx\xxx (.exe文件所在的目錄)

          //獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。
          string str = System.Windows.Forms.Application.ExecutablePath;
          result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)

          //獲取應用程序的當前工作目錄(不可靠)。
          string str = System.IO.Directory.GetCurrentDirectory();
          result: X:\xxx\xxx (.exe文件所在的目錄)

          C# 獲取路徑中,文件名、目錄、擴展名等

          string path = "C:\\dir1\\dir2\\foo.txt";
          string str = "GetFullPath:" + Path.GetFullPath(path) + "\r\n";
          str += "GetDirectoryName:" + Path.GetDirectoryName(path) + "\r\n";
          str += "GetFileName:" + Path.GetFileName(path) + "\r\n";
          str += "GetFileNameWithoutExtension:" + Path.GetFileNameWithoutExtension(path) + "\r\n";
          str += "GetExtension:" + Path.GetExtension(path) + "\r\n";
          str += "GetPathRoot:" + Path.GetPathRoot(path) + "\r\n";
          MessageBox.Show(str);

          結果: 
          GetFullPath:C:\dir1\dir2\foo.txt
          GetDirectoryName:C:\dir1\dir2
          GetFileName:foo.txt
          GetFileNameWithoutExtension:foo
          GetExtension:.txt
          GetPathRoot:C:\

          這里要說明 path 是如何C# 輕松獲取路徑中文件名、目錄、擴展名等判斷目錄和文件名的:它把最后一個 \ 后面的內容當作是文件名。 // 內容來自js4j.com//

          • C:\dir1\dir2\foo.txt 文件名是 foo.txt,目錄名是 C:\dir1\dir2。
          • C:\dir1\dir2\ 文件名是零長度字符串,目錄名是 C:\dir1\dir2。
          • C:\dir1\dir2 文件名是 dir2,目錄名是 C:\dir1。

          posted on 2012-04-23 13:39 SkyDream 閱讀(12377) 評論(0)  編輯  收藏 所屬分類: C# WinForm

          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 陆丰市| 顺平县| 台前县| 平遥县| 六枝特区| 永城市| 玉溪市| 荣昌县| 武定县| 棋牌| 措勤县| 西藏| 乌海市| 铜鼓县| 惠来县| 桃源县| 广水市| 远安县| 封开县| 铜鼓县| 定州市| 乌鲁木齐县| 大余县| 新邵县| 乌海市| 吉首市| 泉州市| 滦平县| 镇平县| 旬阳县| 巴东县| 手游| 凭祥市| 贵德县| 集贤县| 泸溪县| 锦屏县| 青龙| 城市| 鄱阳县| 上饶县|