有没有c#命令可以删除"."在一条小路上
本文关键字:quot 一条 小路 路上 命令 删除 有没有 | 更新日期: 2023-09-27 17:52:36
我有以下内容:
// Get my executable's path
string exeFile = ( new System.Uri( Assembly.GetEntryAssembly().CodeBase ) ).AbsolutePath;
// Get its directory
string exeFolder = Path.GetDirectoryName( exeFile );
// Get rid of those %20 and stuff
exeFolder = Uri.UnescapeDataString( exeFolder );
// Get some relative path from my .config file
string relativePath = ConfigurationManager.AppSettings["MyRelativePath"] );
// Woa. The final result, uff.
string myFinalPath = Path.Combine( exeFolder, relativePath );
现在,如果MyRelativePath类似于..'Xpto'PleaseReadMe.txt
,我的可执行文件位于C:'Ypto'RunMe.exe
,我将以:
C:'Ypto'..'Xpto'PleaseReadMe.txt
C:'Xpto'PleaseReadeMe.txt
考虑到我必须在文本框中显示此路径,当然我不希望用户看到难看的"'Ypto.."部分。我可以很容易地用几行代码创建一个函数来解析这个字符串并删除这些结构,但我想知道是否有一个c#方法可以优雅地完成这项工作。如果没有,你推荐什么算法?
谢谢!
您要找的是Path.GetFullPath()
。
您可以使用Path.GetFullPath
方法。此外,您还可以删除Uri
处理:
// Get my executable's path
string exeFile = Assembly.GetEntryAssembly().Location;
// Get its directory
string exeFolder = Path.GetDirectoryName( exeFile );
// Get some relative path from my .config file
string relativePath = @"..'Xpto'PleaseReadMe.txt";
// Woa. The final result, uff.
string myFinalPath = Path.GetFullPath(Path.Combine(exeFolder, relativePath));
Console.WriteLine(myFinalPath);
样本测试:- C: ' ConsoleApplication1 ' ConsoleApplication1 ' bin '样本。CMD
@DEBUG'ConsoleApplication1.exe
C:'ConsoleApplication1'ConsoleApplication1'bin'Xpto'PleaseReadMe.txt