C# 对桌面的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/802179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# reference to the desktop
提问by
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.
我希望能够在文件名前面标识某种@desktop,它会自动插入桌面路径。这在 C# 中存在吗?或者我是否必须逐台查看计算机上的桌面(OS by OS)。
采纳答案by schnaader
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.
编辑:这适用于 Windows,但Mono 也支持它。
回答by Jon Skeet
You want to use Environment.GetFolderPath, passing in SpecialFolder.DesktopDirectory
.
你想使用Environment.GetFolderPath,传入SpecialFolder.DesktopDirectory
.
There's also SpecialFolder.Desktop
which represents the logicaldesktop location - it's not clear what the difference between the two is though.
还有SpecialFolder.Desktop
which 代表逻辑桌面位置 - 虽然不清楚两者之间的区别是什么。
回答by Chris Van Opstal
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
回答by Marc Gravell
Something like:
就像是:
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"NameOflog file.txt");
tw = new StreamWriter(logPath);
回答by cRichter
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)
但我不建议自动将日志文件写入用户桌面。您应该将文件链接添加到开始菜单文件夹中。甚至在事件日志中填充它们。(好多了)
回答by C. Ross
You want Environment.SpecialFolder
你想要 Environment.SpecialFolder
string fileName = "NameOflog file.txt";
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName);
tw = new StreamWriter(path);
回答by JohnB
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");