zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#中的文件路径获取函数和文件名字获取函数小结

c#文件 函数 获取 路径 小结 名字
2023-06-13 09:15:41 时间

1.获取绝对文件路径

复制代码代码如下:

System.IO.Path.GetFullPath(stringpath)

stringfileName="myfile.ext";
stringpath1=@"mydir";
stringpath2=@"\mydir";
stringfullPath;

fullPath=Path.GetFullPath(path1);
fullPath=Path.GetFullPath(fileName);
fullPath=Path.GetFullPath(path2);


2.获取文件名字(得到指定路径内的文件名,不包括扩展名)
复制代码代码如下:

System.IO.Path.GetFileNameWithoutExtension(stringpath)
stringfileName=@"C:\mydir\myfile.ext";
stringpath=@"C:\mydir\";
stringresult;
result=Path.GetFileNameWithoutExtension(fileName);
result=Path.GetFileName(path);

3.获得包含path目录信息的string或者为空引用
复制代码代码如下:
System.IO.Path.GetDirectoryName(stringpath)

stringfileName=@"C:\mydir\myfile.ext";
stringpath=@"C:\mydir\";
stringrootPath=@"C:\";
stringdirectoryName;
   
directoryName=Path.GetDirectoryName(fileName);
directoryName=Path.GetDirectoryName(path);
directoryName=Path.GetDirectoryName(rootPath);


4.合并两个路径字符串。
复制代码代码如下:
System.IO.Path.Combine(Stringpath1,Stringpath2)