这是我的代码。我正在尝试从存储在音乐文件夹中的.txt文件读取数据。但是我遇到了一些错误, System.NotSupportedException。 不支持指定路径的格式。
请帮助...........
string path = @"Music:\streamfile.txt";
using (StreamReader sr = File.OpenText(path))
{
String s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
Console.ReadLine();
答案 0 :(得分:2)
您可以尝试Environment.GetFolderPath
//if you want windows common music folder ex:C:\Users\Public\Music\streamfile.txt
var CommonMusicPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonMusic) + @"\streamfile.txt";
//if you want windows user music folder ex:C:\Users\username\Music\streamfile.txt
var MyUserMusicPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + @"\streamfile.txt";
using (StreamReader sr = File.OpenText(MyUserMusicPath))
{
String s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
Console.ReadLine();
答案 1 :(得分:2)
在某处有一个带有“特殊”文件夹的列表,但是您可以自己构建它:
string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),
"Music", "streamfile.txt");