Tuesday, May 3, 2011

C# reference to the desktop

I am using a file stream to write out a file.

I was hoping to be able to write the file to the desktop.

If I have something like

tw = new StreamWriter("NameOflog file.txt");

I would like to be able to have some sort of @desktop identified in front of the file name that would automatically insert the path to the desktop. Does this exist in C#? OR do I have to look and see what there desktop is on a computer by computer(OS by OS) basis.

From stackoverflow
  • Quick google search reveals this one:

    string strPath = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
    

    EDIT: This will work for Windows, but Mono supports it, too.

    OregonGhost : Don't add \\ manually. Use Path.Combine instead.
    Marc Gravell : And you don't need to call ToString() on a string
    schnaader : Note that according to http://msdn.microsoft.com/de-de/library/system.environment.specialfolder.aspx this will only work on Windows, so for other OS, you might check if it works first.
    schnaader : @Marc: Thanks, corrected.
    Noldorin : @schnaader: I do believe it also works on Mono/Linux (it should return "/home/username/desktop").
    schnaader : @Noldorin: Thanks for this note, I finally found a link about Mono and Environment.SpecialFolder here: http://www.go-mono.com/docs/index.aspx?link=T%3ASystem.Environment.SpecialFolder
  • You want to use Environment.GetFolderPath, passing in SpecialFolder.DesktopDirectory.

    There's also SpecialFolder.Desktop which represents the logical desktop location - it's not clear what the difference between the two is though.

    TheTXI : Holy cow, Jon Skeet was beat to the punch. Jon, you're not losing your edge on us, are ya buddy? :)
    Jon Skeet : I was busy looking up the links :(
    TheTXI : Come on Jon, you're supposed to have this stuff memorized by now :)
    Jason Miesionczek : The difference may have to do with folder redirection via group policy. Perhaps the SpecialFolder.Desktop refers to the actual location of the folder instead of its normal path on the hard drive
    mkmurray : ...like in setups where your Desktop is stored in your profile, which is then actually physically located up on a central server?
    mkmurray : It appears "logical" might equal "virtual" in the case of `SpecialFolder.Desktop`'s description; see here: http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx I think `DesktopDirectory` is the one to use.
  • Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
    
  • Something like:

        string logPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
            "NameOflog file.txt");
        tw = new StreamWriter(logPath);
    
    Codex : +1 This one works verbatim! ;-)
  • yep. you can use environmental variables. like

    tw = new StreamWriter("%USERPROFILE%\Desktop\mylogfile.txt");
    

    but i would not recommend to automatically write a log file to the users desktop. you should add the link to the file to your start menu folder. or even populate them in the event log. (much better)

  • You want Environment.SpecialFolder

    string fileName = "NameOflog file.txt";
    path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName);
    tw = new StreamWriter(path);
    

0 comments:

Post a Comment