我需要将文件存储到Windows上常见的桌面。该应用程序是只有一个特殊的PC(设备准备)一个非常特殊的应用程序,因此它必须是便于非技术人员的用户查找和修改配置文件。现在,我们切换到域,因为不同的人(有不同的帐户)应使用的软件,它必须是在一个共同的地方,通过每个用户看到。所以,请不要问为什么它在桌面上;)

以前,我只是用Environment.GetFolderPath(Environment.SpecialFolder.Desktop)。有几个在SpecialFolder枚举共同文件夹,但常见桌面似乎不在那里。我失去了一些东西,或者我要的P / Invoke SHGetSpecialFolderPathCSIDL_COMMON_DESKTOPDIRECTORY

有帮助吗?

解决方案

我认为你必须使用SHGetSpecialFolderPath API,因为是“CommonDesktopDirectory”不枚举值。你不能明确地使用CSIDL_COMMON_DESKTOPDIRECTORY的价值,并投它Environment.SpecialFolder,因为GetFolderPath方法检查值在枚举定义。这里的(从反射器)的GetFolderPath方法的代码:

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

所以,你可以轻松地复制和适应的一部分,你需要...

其他提示

有关澄清 - 通过常用的桌面你的意思是?C:\ Documents和Settings \所有用户\桌面

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

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

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

http://msdn.microsoft.com/ EN-US /库/ 0ea7b5xe(VS.85)的.aspx

首先,添加为 “Windows Script Host的对象模型” 的引用。您可以在“添加引用”对话框中的COM选项卡中找到。

using IWshRuntimeLibrary;

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

另一种方式(是的,它也是丑陋的,将可能只工作在Windows XP,而不是Vista系统) 是读取从注册表中的值

HKEY_LOCAL_MACHINE \ SOFTWARE \微软\的Windows \ CurrentVersion \ Explorer中\ Shell文件夹,普通的桌面

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top