Reputation:
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 for desktop paths on a computer by computer (or OS by OS) basis?
Upvotes: 12
Views: 15191
Reputation: 1062770
Something like:
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"NameOflog file.txt");
tw = new StreamWriter(logPath);
Upvotes: 9
Reputation: 18982
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");
Upvotes: 1
Reputation: 49719
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.
Upvotes: 40
Reputation: 31848
You want Environment.SpecialFolder
string fileName = "NameOflog file.txt";
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName);
tw = new StreamWriter(path);
Upvotes: 3
Reputation: 1411
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)
Upvotes: 2
Reputation: 37547
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
Upvotes: 1
Reputation: 1500514
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.
Upvotes: 10