开发者

C# Path类---文件路径解读

开发者 https://www.devze.com 2023-01-25 10:27 出处:网络 作者: Danny_hi
目录C# Path类—文件路径C# 文件路径 Path类 测试总结C# Path类—文件路径 给定如下字符串类型filePath,表示一个文件路径:
目录
  • C# Path类—文件路径
  • C# 文件路径 Path类 测试
  • 总结

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\ERPWork11javascript25\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(p编程ath));              //输出:-----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));         开发者_Go开发       // 输出:-----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();
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号