![](/img/trans.png)
[英]C#: Using StreamReader to read line from txt file, but Peek() return -1 even there are a lot of lines left
[英]I am unable to read content from .txt file using StreamReader class of c#
这是我的代码。我正在尝试从存储在音乐文件夹中的.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();
您可以尝试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();
在某处有一个带有“特殊”文件夹的列表,但是您可以自己构造它:
string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),
"Music", "streamfile.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.