从 Silverlight OutOfBrowser 应用程序访问特殊文件夹

发布于 2024-12-10 14:39:00 字数 737 浏览 0 评论 0原文

在我的 Silverlight 4.0 应用程序(具有更高的信任级别)中,我尝试使用以下代码访问通用模板文件夹:

var folder = Environment.GetFolderPath(Environment.SpecialFolder.Templates);

但是,此类代码会抛出 System.Security.SecurityException:

System.Security.SecurityException was unhandled by user code
  Message=File operation not permitted. Access to path '' is denied.
  StackTrace:
       at System.IO.FileSecurityState.EnsureState()
       at System.Environment.InternalGetFolderPath(SpecialFolder folder, SpecialFolderOption option, Boolean checkHost)
       at System.Environment.GetFolderPath(SpecialFolder folder)

似乎访问除“我的文档”之外的任何文件夹当前用户会抛出这种异常 - 由于 SpecialFolder 枚举有更多值,它们有什么用?有什么方法可以验证此枚举/方法查找哪个文件夹,或者有任何其他方法来访问它?

Inside my Silverlight 4.0 application (with elevated trust level), I'm trying to access the common templates folder, using the code below:

var folder = Environment.GetFolderPath(Environment.SpecialFolder.Templates);

However, such code throwsSystem.Security.SecurityException:

System.Security.SecurityException was unhandled by user code
  Message=File operation not permitted. Access to path '' is denied.
  StackTrace:
       at System.IO.FileSecurityState.EnsureState()
       at System.Environment.InternalGetFolderPath(SpecialFolder folder, SpecialFolderOption option, Boolean checkHost)
       at System.Environment.GetFolderPath(SpecialFolder folder)

It seems that accessing any folder besides "My Documents" for the current user would throw this kind of exception - Since the SpecialFolder enum has more values, what are they good for? Is there any way to verify which folder this enum/method looks for, or any other way to access it?

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

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

发布评论

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

评论(1

子栖 2024-12-17 14:39:00

在 Silverlight 4 中,“我的文档”区域和独立存储是 OOB 应用程序可以任意读取/写入的唯一两个位置。 Silverlight 5 将改变这种情况,提高信任度的应用程序将能够更好地访问磁盘。

至于为什么它在那里,请参阅 MSDN 文档

此类型的存在是为了支持 Silverlight for Windows Phone 中的 .NET Compact Framework 基础结构,并且不适合在您的应用程序代码中使用。

值得注意的是,如果您的目标是 Windows OOB,则可以使用 COM 自动化和 Scripting.FileSystemObject 在 Silverlight 4 中:

using (dynamic fso = AutomationFactory.CreateObject("Scripting.FileSystemObject"))
{
    dynamic file = fso.CreateTextFile(@"C:\tmp.txt");
    file.WriteLine(@"I just wrote to c:\ !!");
    file.Close();
}

In Silverlight 4, the "My Documents" area and isolated storage are the only two places that OOB apps my arbitrarily read/write from/to. This will change with Silverlight 5, where elevated trust apps will have greater access to disk.

As for why it's there at all, see the remark in the MSDN Documentation:

This type is present to support the .NET Compact Framework infrastructure in Silverlight for Windows Phone, and it is not intended to be used in your application code.

It's worth noting that if you are targeting windows OOB, it is possible to read/write files arbitrarily on disk using COM automation and Scripting.FileSystemObject in Silverlight 4:

using (dynamic fso = AutomationFactory.CreateObject("Scripting.FileSystemObject"))
{
    dynamic file = fso.CreateTextFile(@"C:\tmp.txt");
    file.WriteLine(@"I just wrote to c:\ !!");
    file.Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文