我无法使用 c# 的 StreamReader class 从 .txt 文件中读取内容
I am unable to read content from .txt file using StreamReader class of c#
这是我的代码..我正在尝试从存储在 music 文件夹中的 .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();
某处有一个包含 'special' 个文件夹的列表,但您可以自己构建它:
string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),
"Music", "streamfile.txt");
这是我的代码..我正在尝试从存储在 music 文件夹中的 .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();
某处有一个包含 'special' 个文件夹的列表,但您可以自己构建它:
string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),
"Music", "streamfile.txt");