1

这是我的代码..我正在尝试从存储在音乐文件夹中的 .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();
4

2 回答 2

2

某处有一个带有“特殊”文件夹的列表,但您可以自己构建它:

  string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),
           "Music", "streamfile.txt");
于 2018-06-29T10:08:21.527 回答
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();
于 2018-06-29T10:07:32.660 回答