无法更新和保存网页.配置文件
本文关键字:保存网页 配置文件 更新 | 更新日期: 2023-09-27 18:10:44
我尝试加密网络的连接字符串部分。配置文件。Web应用程序通过一个简单的winforms应用程序。我以管理员的身份运行VS2010,并运行以下代码。
private void encryptButton_Click(object sender, EventArgs e)
{
Configuration config = GetConfiguration();
ConfigurationSection configSection = config.GetSection("connectionStrings");
if (configSection != null)
// Only encrypt the section if it is not already protected
if (!configSection.SectionInformation.IsProtected)
{
configSection.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
configSection.SectionInformation.ForceSave = true;
config.Save();
DisplayWebConfig();
}
}
private Configuration GetConfiguration()
{
var configFile = new FileInfo(@"C:'Users'abalawan'Desktop'CN'R13 new'Websites'ABC");
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName,true,configFile.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/",vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}
由于我只有物理路径,我试图将路径映射到相应的虚拟路径。但是config.Save()给了我以下错误
ConfigurationErrorsException was unhandled.
Unable to save config to file 'C:'Users'abalawan'Desktop'CN'R13 new'Websites'ABC'.
为了清晰起见,下面是StackTrace的样子
at System.Configuration.Internal.WriteFileContext.ReplaceFile(String Source, String Target)
at System.Configuration.Internal.WriteFileContext.Complete(String filename, Boolean success)
at System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.UpdateConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
at System.Configuration.Configuration.Save()
at EncryptionandDecryptionApplication.Form1.encryptButton_Click(Object sender, EventArgs e) in C:'Users'abalawan'Desktop'CN'EncryptionandDecryptionApplication'EncryptionandDecryptionApplication'Form1.cs:line 55
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EncryptionandDecryptionApplication.Program.Main() in C:'Users'abalawan'Desktop'CN'EncryptionandDecryptionApplication'EncryptionandDecryptionApplication'Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
你的代码是完美的。似乎有许可问题。暂时试着让所有人都能访问你的文件位置。如果它工作,那么它的权限问题(虽然给每个人完全的权限不是一个好主意)
如果你想在你的应用程序中添加UAC权限,那么你可以添加app.manifest文件,并在该文件中更改requestdexecutionlevel标签,如下所示:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />