Tutorial :C# reference to the desktop



Question:

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.


Solution:1

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.


Solution:2

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.


Solution:3

Something like:

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


Solution:4

You want Environment.SpecialFolder

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


Solution:5

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)


Solution:6

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))  


Solution:7

I also use the method mentioned above.

But here are a couple different options that work too (just to have a more comprehensive list):

using System;  using System.Runtime.InteropServices;  using System.Text;    class Program  {    // 1st way    private const int MAX_PATH = 260;    private const int CSIDL_DESKTOP = 0x0000;    private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019; // Can get to All Users desktop even on .NET 2.0,                                                              // where Environment.SpecialFolder.CommonDesktopDirectory is not available    [DllImport("shell32.dll")]    private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, int nFolder, bool fCreate);    static string GetDesktopPath()    {      StringBuilder currentUserDesktop = new StringBuilder(MAX_PATH);      SHGetSpecialFolderPath((IntPtr)0, currentUserDesktop, CSIDL_DESKTOP, false);      return currentUserDesktop.ToString();    }      // 2nd way    static string YetAnotherGetDesktopPath()    {      Guid PublicDesktop = new Guid("C4AA340D-F20F-4863-AFEF-F87EF2E6BA25");      IntPtr pPath;        if (SHGetKnownFolderPath(PublicDesktop, 0, IntPtr.Zero, out pPath) == 0)      {        return System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);      }        return string.Empty;    }  }  

Then:

string fileName = Path.Combine(GetDesktopPath(), "NameOfLogFile.txt");  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »