简体   繁体   English

我无法使用C#的StreamReader类从.txt文件读取内容

[英]I am unable to read content from .txt file using StreamReader class of c#

Here is my code..I am trying to read data from .txt file which was stored in music folder. 这是我的代码。我正在尝试从存储在音乐文件夹中的.txt文件读取数据。 But i am getting some error like, System.NotSupportedException. 但是我遇到了一些错误,例如System.NotSupportedException。 The given path's format is not supported. 不支持给定路径的格式。

Please help........... 请帮忙...........

string path = @"Music:\streamfile.txt";

using (StreamReader sr = File.OpenText(path))
{
    String s = "";

    while ((s = sr.ReadLine()) != null)
    {
        Console.WriteLine(s);
    }
}
Console.ReadLine();

you can try Environment.GetFolderPath 您可以尝试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();

There is a list with 'special' folders somewhere but you can construct it yourself: 在某处有一个带有“特殊”文件夹的列表,但是您可以自己构造它:

  string path = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"),
           "Music", "streamfile.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM