C# 桌面参考

发布于 2024-07-18 09:13:16 字数 226 浏览 10 评论 0原文

我正在使用文件流写出文件。

我希望能够将文件写入桌面。

如果我有类似的东西,

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

我希望能够在文件名前面标识某种@desktop,它会自动插入桌面路径。

C#中存在这个吗? 或者我是否必须逐台计算机(或逐个操作系统)寻找桌面路径?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我也只是我 2024-07-25 09:13:16

快速谷歌搜索揭示了这一点:

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

编辑:这适用于Windows,但Mono 也支持

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.

三五鸿雁 2024-07-25 09:13:16

您想要使用 Environment.GetFolderPath,传入 < a href="http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx" rel="noreferrer">SpecialFolder.DesktopDirectory

还有 SpecialFolder.Desktop 代表逻辑桌面位置 - 但尚不清楚两者之间的区别是什么。

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.

月隐月明月朦胧 2024-07-25 09:13:16

就像是:

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

Something like:

string logPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
    "NameOflog file.txt");
tw = new StreamWriter(logPath);
静水深流 2024-07-25 09:13:16

你想要Environment.SpecialFolder

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

You want Environment.SpecialFolder

string fileName = "NameOflog file.txt";
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName);
tw = new StreamWriter(path);
难得心□动 2024-07-25 09:13:16

是的。
您可以使用环境变量。
喜欢

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

,但我不建议自动将日志文件写入用户桌面。
您应该将该文件的链接添加到开始菜单文件夹中。
甚至将它们填充到事件日志中。 (好多了)

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)

遮了一弯 2024-07-25 09:13:16
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory))
信仰 2024-07-25 09:13:16

我也使用上面提到的方法。

但这里有几个不同的选项也有效(只是为了有一个更全面的列表):

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;
  }
}

然后:

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文