![](/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.