将路径保存为相对路径 [英] Save path to a Relative Path
问题描述
我有3个文件路径.从colorFile.xml生成或保存文件路径相对路径的健壮逻辑是什么?
I have 3 filepaths. Whats the robust logic to make or save filepaths relative paths in code from the colorFile.xml .
以下电流:
c:\ temp \ colors \ colorFile.xml
c:\temp\colors\colorFile.xml
c:\ temp \ colors \ output \
c:\temp\colors\output\
c:\ temp \ colors \ input \
c:\temp\colors\input\
我正在努力实现的输出:
OUTPUT I AM TRYING TO ACHIEVED:
.. \ .. \ .. \ colorFile.xml
..\..\..\colorFile.xml
.. \ .. \ .. \ output \
..\..\..\output\
.. \ .. \ colors \ input \
..\..\colors\input\
pianoboyCoder
pianoboyCoder
推荐答案
painboyCoder,
Hi painoboyCoder,
谢谢您在这里发布.
对于您的问题,三个文件路径的xml不是xml文件的正确格式.因此,例如,我在.txt文件中放置了三个文件路径.
For your question, the xml of three filepaths is not a correct format of xml file. Hence I put three filepaths in .txt file for example.
请尝试以下代码.
static void Main(string[] args)
{
string[] filepaths = File.ReadAllLines(@"C:\Users\v-wezan\Desktop\colorFile.txt");
List<string> relativePaths = new List<string>();
foreach (var item in filepaths)
{
Uri file = new Uri(item);
Uri folder = new Uri(@"C:\Users\v-wezan\Desktop\temp\colors\");
string relativePath = folder.MakeRelativeUri(file).ToString();
relativePaths.Add(relativePath.Replace("/","\\"));
}
foreach (var item in relativePaths)
{
Console.WriteLine("The relativePaths is {0}.", @"..\..\..\" + item);
}
Console.ReadKey();
}
这是.txt文件中的文件.
Here is the file in .txt file.
这是输出.
我希望这会对您有所帮助.
I hope this would be helpful to you.
最好的问候,
温迪
这篇关于将路径保存为相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!