首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Environment.SpecialFolder.CommonDocuments

Environment.SpecialFolder.CommonDocuments
EN

Stack Overflow用户
提问于 2012-07-06 11:37:05
回答 2查看 8.2K关注 0票数 4

当我执行以下语句时:

代码语言:javascript
复制
string folderPath = 
   Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);

folderPath设置为C:\ProgramData

在“即时”窗口中执行此语句时:

代码语言:javascript
复制
Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);

将显示C:\Users\Public\Documents (这是我所期望的)。

有什么不同的想法吗?

更新7/6/12:

我在同一个exe中的不同类中得到了不同的结果。

我有一个类存在于库中,还有一个类直接链接到应用程序。

库类返回“C:\ProgramData”。链接的代码返回“C:\Users\Public\Documents”。

此外,对于“Environment.SpecialFolder.CommonDocuments”和“Environment.SpecialFolder.ApplicationData”,库代码都返回“C:\ProgramData”。

链接的代码为“Environment.SpecialFolder.CommonDocuments”返回“C:\Users\Public\Documents”,为“Environment.SpecialFolder.ApplicationData”返回"C:\Users\Me\AppData\Roaming“。

我很困惑。

EN

回答 2

Stack Overflow用户

发布于 2012-07-06 14:02:28

如果您的程序是64位的,则可能会发生这种情况。由于Visual Studio是32位的,因此当您在“即时”窗口中执行Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);时,它将查找Windows32配置单元,而您的程序将查找64配置单元。CommonDocuments文件夹可能已被移动,该文件夹只会在64配置单元中注册。

这是一个定义为here的Windows

你的更新说它发生在同一个EXE中的两个类中。由于一个进程只能是32位或64位(不能同时是32位或64位),这将表明上述错误不适用于您(假设程序集之间的正常通信,而不是具有包装的COM )。你能把它写成一个合适的测试来发布吗?

作为快速确认,可能还值得在每个进程中包含以下代码,以确保它们都在同一进程中运行:

代码语言:javascript
复制
  Console.WriteLine("{0} Process {1} is {2}bit", GetType().ToString(), System.Diagnostics.Process.GetCurrentProcess().Id, IntPtr.Size * 8);
票数 4
EN

Stack Overflow用户

发布于 2012-07-06 11:45:49

C:\Users\Public\Documents是正确的路径:

代码语言:javascript
复制
Per Machine “Documents”
“Document” type files that users create/open/close/save in the application that are used across users.  These are usually template or public documents.

Example:    MyTemplate.dot
Windows 7:  C:\Users\Public
Vista:  %SystemDrive%\Users\Public
XP: %ALLUSERSPROFILE%\Documents
Environment Variable:   Vista/Win7: %PUBLIC%  Note: Does not exist on XP
Known Folder ID:    FOLDERID_PublicDocuments
System.Environment.SpecialFolder:   System.Environment.SpecialFolder.CommonDocuments
CSIDL:  CSIDL_COMMON_DOCUMENTS
It’s obvious after looking at all these locations that where you store your files can be challenging if you are targeting multiple OS versions.  The best guidance is to use API’s to find the special folder path.  API’s will return the appropriate location for the target OS.

来源:http://blogs.msdn.com/b/patricka/archive/2010/03/18/where-should-i-store-my-data-and-configuration-files-if-i-target-multiple-os-versions.aspx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11355484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档