获取.NET中的通用桌面路径

发布于 2024-08-01 23:45:10 字数 422 浏览 6 评论 0原文

我需要将文件存储到 Windows 上的公共桌面中。 该应用程序是针对一台特殊 PC(设备准备)的非常特殊的应用程序,因此非技术用户必须能够轻松查找和修改配置文件。 现在我们切换到域,并且由于不同的人(具有不同的帐户)应该使用该软件,因此它必须位于公共位置,每个用户都可以看到。 所以请不要问为什么它在桌面上;)

以前,我只是使用Environment.GetFolderPath(Environment.SpecialFolder.Desktop)SpecialFolder 枚举中有几个常用文件夹,但常用桌面似乎不存在。 我是否遗漏了某些内容,或者我是否必须使用 CSIDL_COMMON_DESKTOPDIRECTORY p/调用 SHGetSpecialFolderPath

I need to store files into the common desktop on Windows. The application is a very special application for exactly one special PC (device preparation), so it had to be easy for non-techie users to find and modify the configuration file. Now we switched to a domain, and because different people (with different accounts) should use the software, it has to be in a common place, seen by every user. So please don't ask why it's on the desktop ;)

Previously, I just used Environment.GetFolderPath(Environment.SpecialFolder.Desktop). There are several of the common folders in the SpecialFolder enumeration, but the common desktop seems not to be there. Am I missing something, or do I have to p/invoke SHGetSpecialFolderPath with CSIDL_COMMON_DESKTOPDIRECTORY?

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

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

发布评论

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

评论(5

野生奥特曼 2024-08-08 23:45:10

我认为您必须使用 SHGetSpecialFolderPath API,因为“CommonDesktopDirectory”没有枚举值。 您无法显式使用 CSIDL_COMMON_DESKTOPDIRECTORY 的值并将其转换为 Environment.SpecialFolder,因为 GetFolderPath 方法会检查该值是否在枚举。 这是 GetFolderPath 方法的代码(来自 Reflector):

public static string GetFolderPath(SpecialFolder folder)
{
    if (!Enum.IsDefined(typeof(SpecialFolder), folder))
    {
        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GetResourceString("Arg_EnumIllegalVal"), new object[] { (int) folder }));
    }
    StringBuilder lpszPath = new StringBuilder(260);
    Win32Native.SHGetFolderPath(IntPtr.Zero, (int) folder, IntPtr.Zero, 0, lpszPath);
    string path = lpszPath.ToString();
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
    return path;
}

因此您可以轻松复制和调整您需要的部分...

I think you have to use the SHGetSpecialFolderPath API, since there is no enum value for "CommonDesktopDirectory". You can't explicitly use the value of CSIDL_COMMON_DESKTOPDIRECTORY and cast it to Environment.SpecialFolder, because the GetFolderPath method checks that the value is defined in the enum. Here's the code of the GetFolderPath method (from Reflector) :

public static string GetFolderPath(SpecialFolder folder)
{
    if (!Enum.IsDefined(typeof(SpecialFolder), folder))
    {
        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GetResourceString("Arg_EnumIllegalVal"), new object[] { (int) folder }));
    }
    StringBuilder lpszPath = new StringBuilder(260);
    Win32Native.SHGetFolderPath(IntPtr.Zero, (int) folder, IntPtr.Zero, 0, lpszPath);
    string path = lpszPath.ToString();
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
    return path;
}

So you can easily copy and adapt the part that you need...

落花随流水 2024-08-08 23:45:10

澄清一下 - 通用桌面是指 C:\Documents and Settings\All Users\Desktop 吗?

如果是的话,这是一个丑陋的黑客 -

Dim c As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim comDesktop As String = c.Substring(0, c.LastIndexOf("\")) + "\Desktop"

For clarification - By common desktop do you mean C:\Documents and Settings\All Users\Desktop?

If yes, this is an ugly hack -

Dim c As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim comDesktop As String = c.Substring(0, c.LastIndexOf("\")) + "\Desktop"
留一抹残留的笑 2024-08-08 23:45:10

您可以使用 Windows 脚本宿主 - WshShell.SpecialFolders

http:/ /msdn.microsoft.com/en-us/library/0ea7b5xe(VS.85).aspx

You can use Windows Script Host - WshShell.SpecialFolders

http://msdn.microsoft.com/en-us/library/0ea7b5xe(VS.85).aspx

泪是无色的血 2024-08-08 23:45:10

首先,添加对“Windows 脚本宿主对象模型”的引用。 您可以在“添加引用”对话框的 COM 选项卡中找到它。

using IWshRuntimeLibrary;

object commonUserDesktop = "AllUsersDesktop";
WshShell shell = new WshShellClass();
string commonPath = shell.SpecialFolders.Item(ref commonUserDesktop).ToString();

First, add a reference to "Windows Script Host Object Model". You'll find this in the COM tab of the "Add References" dialog.

using IWshRuntimeLibrary;

object commonUserDesktop = "AllUsersDesktop";
WshShell shell = new WshShellClass();
string commonPath = shell.SpecialFolders.Item(ref commonUserDesktop).ToString();
凌乱心跳 2024-08-08 23:45:10

另一种方式(是的,它也很丑陋,可能只能在 Windows XP 上运行,而不能在 Vista 上运行)
读取一个值

是从注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders 、 Common Desktop 中

Another way (yes it is also ugly and will work probably only on Windows XP, not on Vista)
is to read a value from registry

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders , Common Desktop

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