.NET“设置”无法在 64 位上运行(InvalidOperationException)

发布于 2024-10-19 16:53:47 字数 1320 浏览 4 评论 0原文

我正在构建一个 C# .NET (VS2010) 应用程序,但在保存设置时遇到一些问题(在 32 位 Windows XP 下工作正常),在 64 位 Windows 7 下。

我在解决方案中有一个 Settings.settings,其设置测试值名为res112text 中,在用户范围内设置为字符串类型,并使用“Internal”作为访问修饰符设置。

然后,当捕获时,保存命令

Properties.Settings.Default.res112text = "10002b";
try
{
   Properties.Settings.Default.Save();
}
catch (Exception e)
{
   MessageBox.Show(e.GetType().ToString() + " for " + e.Message.ToString());
}

立即显示错误类型:

System.InvalidOperationException

对于异常消息,它显示:

方法失败,出现意外错误代码 3

我正在将 .dll.config 文件打包到安装程序中,并且它可以很好地安装到 Program Files 目录中。

谁能建议可能出了什么问题吗?

更新:完整的错误是..

Error System.InvalidOperationException: Method failed with unexpected error 
code 3. 

at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(
ResourceType resourceType, Boolean isContainer, String name, SafeHandle 
handle, AccessControlSections includeSections, Boolean createByName, 
ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)

at System.Security.AccessControl.FileSecurity..ctor(String fileName, 
AccessControlSections includeSections)

...

at System.Configuration.SettingsBase.Save()

at MyAddon.IEModule.ConfigSave()

更新:启用 IE 保护模式后似乎没有解决方法,除非所有用户手动将其关闭。

I am building a C# .NET (VS2010) application but am having some trouble saving settings, (which work fine under 32bit Windows XP), under 64bit Windows 7.

I have a Settings.settings in the solution, with a settings test value named res112text in, set as type string at user scope, with 'Internal' as the access modifier setting.

Then the save command in

Properties.Settings.Default.res112text = "10002b";
try
{
   Properties.Settings.Default.Save();
}
catch (Exception e)
{
   MessageBox.Show(e.GetType().ToString() + " for " + e.Message.ToString());
}

when caught instantly brings up the error type:

System.InvalidOperationException

and for the exception message it says:

Method failed with unexpected error code 3

I am packing the .dll.config file in the setup installer and it installs into the Program Files directory fine.

Can anyone suggest what might be going wrong please?

Update: The full error is..

Error System.InvalidOperationException: Method failed with unexpected error 
code 3. 

at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(
ResourceType resourceType, Boolean isContainer, String name, SafeHandle 
handle, AccessControlSections includeSections, Boolean createByName, 
ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext)

at System.Security.AccessControl.FileSecurity..ctor(String fileName, 
AccessControlSections includeSections)

...

at System.Configuration.SettingsBase.Save()

at MyAddon.IEModule.ConfigSave()

Update: It appears there isn't a workaround when IE's Protected Mode is enabled, unless all users switch it off manually.

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

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

发布评论

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

评论(2

故人的歌 2024-10-26 16:53:47

您的问题可能是由于没有足够的权限来保存文件而引起的。就像 Hans Passant 建议的那样,您应该看看独立存储: http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx

隔离存储的目的是允许权限有限的应用程序保存数据。 MSDN 页面包含一个代码示例,可以帮助您启动并运行。

Your problem is probably caused because of insufficient rights to save the file. Like Hans Passant suggested, you should take a look at Isolated storage: http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx

The purpose of isolated storage is to allow applications with limited rights to save data. The MSDN page contains a code sample that should get you up-and-running.

挽你眉间 2024-10-26 16:53:47

如果没有 DLL“user.config”文件,我可以使用 Save() 方法,但如果已经有 DLL“user.config”文件,我就不能执行相同的操作。因此,解决方法可能是手动删除“user.config”文件(这很奇怪)

System.IO.File.Delete(System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoaming).FilePath);

,然后使用 Save() 方法创建一个新的“user.config”文件。
如果有问题请告诉我,但到目前为止我还没有注意到。

I can use Save() method if there is no DLL "user.config" file, but I can't do the same when there is one already. So the workaround is probably to delete the "user.config" file manually (which is curiously working)

System.IO.File.Delete(System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoaming).FilePath);

and then use the Save() method to create a new "user.config" file.
Please let me know if there is a catch, but so far I haven't noticed any.

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