C# Path类—文件路径
给定如下字符串类型filePath,表示一个文件路径:
1
|
string filePath = "D:\\Program\\Test\\Config.txt" ; |
下面是Path类中的一些常用方法及执行的结果:
1
2
3
4
5
6
7
8
9
10
11
|
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:\ |
获取当前的程序目录:
1
2
3
4
5
|
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类做一个系统的测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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