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


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();





I agree - @"Music:streamfile.txt"; doesn't look like a valid path; where is it actually. Note: the current user's Music folder is: %USERPROFILE%Music, so you might want @"%USERPROFILE%Musicstreamfile.txt" - but you might need to do an env expand first, not sure - try it!
– Marc Gravell
Jun 29 at 9:57



@"Music:streamfile.txt";


Music


%USERPROFILE%Music


@"%USERPROFILE%Musicstreamfile.txt"





ThisPC > Music in windows 10
– Kalpesh Patil
Jun 29 at 10:00





"ThisPC > Music" is simply a lie to keep things simple for casual users; you probably mean %USERPROFILE%Music
– Marc Gravell
Jun 29 at 10:01


%USERPROFILE%Music





Go to the file system and see the properties of the file. There you can see the real path. Use that one. The music folder is an abstraction not available to your code.
– Afonso
Jun 29 at 10:02





Actually i dont have Local Folder that's why i placed my file in that folder.
– Kalpesh Patil
Jun 29 at 10:03




2 Answers
2



There is a list with 'special' folders somewhere but you can construct it yourself:


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





OMG...It's Working. I can see my data ....Thank You so much sir..
– Kalpesh Patil
Jun 29 at 10:13



you can try Environment.GetFolderPath


Environment.GetFolderPath


//if you want windows common music folder ex:C:UsersPublicMusicstreamfile.txt
var CommonMusicPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonMusic) + @"streamfile.txt";
//if you want windows user music folder ex:C:UsersusernameMusicstreamfile.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();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift