获取.NET中的通用桌面路径
我需要将文件存储到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您必须使用
SHGetSpecialFolderPath
API,因为“CommonDesktopDirectory”没有枚举值。 您无法显式使用CSIDL_COMMON_DESKTOPDIRECTORY
的值并将其转换为Environment.SpecialFolder
,因为GetFolderPath
方法会检查该值是否在枚举。 这是GetFolderPath
方法的代码(来自 Reflector):因此您可以轻松复制和调整您需要的部分...
I think you have to use the
SHGetSpecialFolderPath
API, since there is no enum value for "CommonDesktopDirectory". You can't explicitly use the value ofCSIDL_COMMON_DESKTOPDIRECTORY
and cast it toEnvironment.SpecialFolder
, because theGetFolderPath
method checks that the value is defined in the enum. Here's the code of theGetFolderPath
method (from Reflector) :So you can easily copy and adapt the part that you need...
澄清一下 - 通用桌面是指 C:\Documents and Settings\All Users\Desktop 吗?
如果是的话,这是一个丑陋的黑客 -
For clarification - By common desktop do you mean C:\Documents and Settings\All Users\Desktop?
If yes, this is an ugly hack -
您可以使用 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
首先,添加对“Windows 脚本宿主对象模型”的引用。 您可以在“添加引用”对话框的 COM 选项卡中找到它。
First, add a reference to "Windows Script Host Object Model". You'll find this in the COM tab of the "Add References" dialog.
另一种方式(是的,它也很丑陋,可能只能在 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