短处理路径名
本文关键字:路径名 处理 | 更新日期: 2023-09-27 17:51:01
目前我的文件路径是
@"S:'Temporary Shared Space'Project'Boyscout3'Data'full_pack.txt"
我希望它只是"full_pack.txt"。
我在底部尝试了这个代码,但它似乎不适合我:
如果有人能帮忙的话,我会很感激的。
string fileName =@"S:'Temporary Shared Space'Project'Boyscout'Data'full_pack.txt";
string path = "full_pack.txt";
string result;
result = System.IO.Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result);
result = System.IO.Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result);
//filename shorter for den1
string fileName1 =@"S:'Temporary Shared Space'Project'Boyscout3'Data'den1.txt";
string path1 = "den1.txt";
string result1;
result1 = System.IO.Path.GetFileName(fileName1);
Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName1, result1);
result1 = System.IO.Path.GetFileName(path1);
Console.WriteLine("GetFileName('{0}') returns '{1}'", path1, result1);
//Making a list for full_pack
List<string> listFullPack = new List<string>();
string line;
StreamReader sr = new StreamReader("full_pack.txt");//this is where things don't work.
while ((line = sr.ReadLine()) != null)////When the full address is in, the code works,
{ //but when I replace it with full_pack.txt,
listFullPack.Add(line); //it can't find the file.
}
sr.Close();
StreamReader sr = new StreamReader("full_pack.txt");
行将在可执行目录中查找此文件。你需要在文件名后面加上完整的路径。
您正在使用库中的错误方法。
尝试System.IO.Path.GetFileName
并查看文档