简短的路径名称

时间:2015-05-27 16:22:05

标签: c#

目前我有我的文件路径

 @"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();

1 个答案:

答案 0 :(得分:0)

StreamReader sr = new StreamReader("full_pack.txt");将在此文件的可执行文件目录中查找。您需要在顶部附加文件名的完整路径。

您使用的是库中的错误方法。

尝试System.IO.Path.GetFileName并查看docs