简体   繁体   中英

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. But i am getting some error like, 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

//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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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