简体   繁体   English

无法更新和保存web.config文件

[英]Unable to update and save web.config file

I tried to encrypt the connection string section of the web.config file of a ASP.Net Web application via a simple winforms application. 我试图通过一个简单的winforms应用程序对ASP.Net Web应用程序的web.config文件的连接字符串部分进行加密。 I ran the VS2010 as an administrator and ran the following section of code. 我以管理员身份运行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, "/");
        }

As I had only the physical path I tried to map the path accordingly to a virtual path. 由于只有物理路径,因此尝试将路径相应地映射到虚拟路径。 But config.Save() is throwing me the following error 但是config.Save()向我抛出以下错误

ConfigurationErrorsException was unhandled.

Unable to save config to file 'C:\Users\abalawan\Desktop\CN\R13 new\Websites\ABC'.

Just for the sake of clarity this is how the StackTrace looks like 为了清楚起见,这就是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()

your code is perfect. 您的代码是完美的。 There seems to be permission issue. 似乎有权限问题。 Just for time being try giving full access to your file location to everyone. 只是暂时尝试让所有人都可以完全访问您的文件位置。 If it works then its the permission issue(Though giving full permission to everyone isn't a good idea ) 如果可行,则是权限问题(尽管对所有人授予完全权限不是一个好主意)

If you want to add UAC permission in your application then you can add app.manifest file and in that file changes the requestedExecutionLevel tag as below: 如果要在应用程序中添加UAC权限,则可以添加app.manifest文件,然后在该文件中更改requestedExecutionLevel标记,如下所示:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />                    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM