網站首頁 編程語言 正文
C# Path類—文件路徑
給定如下字符串類型filePath,表示一個文件路徑:
string filePath = "D:\\Program\\Test\\Config.txt";
下面是Path類中的一些常用方法及執行的結果:
Path.GetFullPath(filePath); ? //執行結果為==>"D:\\Program\\Test\\Config.txt";
Path.GetDirectoryName(filePath); //執行結果為==>D:\\Program\\Test
Path.GetFileName(filePath); ?//執行結果為==>Config.txt
Path.GetFileNameWithoutExtension(filePath); //執行結果為==>Config
Path.GetExtension(filePath); //執行結果為==>.txt
Path.GetPathRoot(filePath); //執行結果為==>D:\
獲取當前的程序目錄:
AppDomain.CurrentDomain.BaseDirectory;?? ?//執行結果==>"D:\\Program\\Test\\Bin\\Debug\\"
Application.StartupPath;?? ?//執行結果==>"D:\\Program\\Test\\Bin\\Debug"
Environment.CurrentDirectory;//獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄
Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執行的exe的文件名
C# 文件路徑 Path類 測試
騰出點時間對Path類做一個系統的測試
? ? private void PathTest()
? ? ? ? {
? ? ? ? ? ? //------------必須的空間-------using System.Diagnostics; ? ? using System.IO;
? ? ? ? ? ? string path = @"C:\Users\cks\Desktop\zzg\ERPWork1125\User.lua";
? ? ? ? ? ? Debug.Print(Path.ChangeExtension(path, "txt")); ? ? ? ? ? ? ? ? ? ? // 輸出:-----C:\Users\cks\Desktop\zzg\ERPWork1125\User.txt
? ? ? ? ? ? string path1 = @"C:\Users\cks\Desktop\zzg";
? ? ? ? ? ? string path2 = @"gg/e.txt";
? ? ? ? ? ? Debug.Print(Path.Combine(path1, path2)); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----C:\Users\cks\Desktop\zzg\gg/e.txt
? ? ? ? ? ? Debug.Print(Path.GetDirectoryName(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? //輸出:-----C:\Users\cks\Desktop\zzg\ERPWork1125
? ? ? ? ? ? Debug.Print(Path.GetExtension(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //輸出:-----.lua
? ? ? ? ? ? Debug.Print(Path.GetFileName(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----User.lua
? ? ? ? ? ? Debug.Print(Path.GetFileNameWithoutExtension(path)); ? ? ? ? ? ? ? ?//輸出:-----User
? ? ? ? ? ? Debug.Print(Path.GetFullPath(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 輸出:-----C:\Users\cks\Desktop\zzg\ERPWork1125\User.lua
? ? ? ? ? ? Debug.Print(String.Join("/x/", Path.GetInvalidFileNameChars())); ? ?//輸出:-----"/x/</x/>/x/|/x/
? ? ? ? ? ? Debug.Print(String.Join("/a/", Path.GetInvalidPathChars())); ? ? ? ?//輸出:----- " /a/</a/>/a/|/a/
? ? ? ? ? ? Debug.Print(Path.GetPathRoot(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----C:\
? ? ? ? ? ? Debug.Print(Path.GetRandomFileName()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----0am13z3o.gzd
? ? ? ? ? ? Debug.Print(Path.GetTempFileName()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----C:\Users\cks\AppData\Local\Temp\tmp81E5.tmp
? ? ? ? ? ? Debug.Print(Path.HasExtension(path).ToString()); ? ? ? ? ? ? ? ? ? ?//輸出:-----True
? ? ? ? ? ? Debug.Print(Path.IsPathRooted(path).ToString()); ? ? ? ? ? ? ? ? ? ?//輸出:-----True
? ? ? ? ? ? Debug.Print(Path.AltDirectorySeparatorChar.ToString()); ? ? ? ? ? ? //輸出:-----/
? ? ? ? ? ? Debug.Print(Path.DirectorySeparatorChar.ToString()); ? ? ? ? ? ? ? ?// ?輸出:-----\
? ? ? ? ? ? Debug.Print(String.Join("/x/", Path.InvalidPathChars)); ? ? ? ? ? ? //輸出:-----"/x/</x/>/x/|/x/
? ? ? ? ? ? Debug.Print(Path.PathSeparator.ToString()); ? ? ? ? ? ? ? ? ? ? ? ? //輸出:-----;
? ? ? ? ? ? Debug.Print(Path.VolumeSeparatorChar.ToString()); ? ? ? ? ? ? ? ? ? //輸出:-----: ?
? ? ? ? ? ? // LuaDLL.getc(stdin);
? ? ? ? ? ? //test t = new test();
? ? ? ? }?
總結
原文鏈接:https://blog.csdn.net/qq_43024228/article/details/120931210
相關推薦
- 2023-07-16 oracle 創建存儲過程
- 2022-04-21 Android?App頁面滑動標題欄顏色漸變詳解_Android
- 2022-09-12 iOS開發TableView網絡請求及展示預加載實現示例_IOS
- 2022-04-18 python全面解析接口返回數據_python
- 2022-07-07 Pytorch卷積神經網絡遷移學習的目標及好處_python
- 2023-07-26 vscode中配置代碼片段
- 2022-05-01 你需要掌握的20個Python常用技巧_python
- 2022-04-18 Python?字典(Dictionary)詳細介紹_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支