日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#?Path類---文件路徑解讀_C#教程

作者:Danny_hi ? 更新時間: 2023-03-20 編程語言

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

欄目分類
最近更新