“访问路径被拒绝”问题

时间:2014-05-20 12:38:56

标签: c# winforms

1)我试图以编程方式创建目录

if (!Directory.Exists(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\VoiceRecords"))
    {
Directory.CreateDirectory(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\VoiceRecords");
    }

当我在调试模式下运行我的应用程序时,上面的代码工作正常,但在创建安装程序后,当我安装时,然后当上面的行执行并尝试在我的应用程序安装位置创建文件夹时,问题就开始了。什么是摆脱这个问题的最佳方法,因为我将这个设置分发给第三部分,他们可以安装我的应用程序任何我可能不知道的地方。

2)当我尝试在app.config文件中保存任何值时,我收到错误。 错误屏幕截图在这里。 enter image description here

现在我想展示我如何在运行时写入数据

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                        if (config.AppSettings.Settings["localVoiceRecordsPath"].Value != null || config.AppSettings.Settings["localVoiceRecordsPath"].Value.Trim() != "")
                        {

                            config.AppSettings.Settings["localVoiceRecordsPath"].Value = SettingsPopup.txtPath.Text.Replace(",", string.Empty);
                            config.Save(ConfigurationSaveMode.Modified);
                            SettingsPopup.txtPath.Text = config.AppSettings.Settings["localVoiceRecordsPath"].Value.Replace(",", string.Empty);
                        }
                        else
                        {
                            config.AppSettings.Settings.Add("localVoiceRecordsPath", SettingsPopup.txtPath.Text.Replace(",", string.Empty));
                            config.Save(ConfigurationSaveMode.Modified);
                            SettingsPopup.txtPath.Text = config.AppSettings.Settings["localVoiceRecordsPath"].Value.Replace(",", string.Empty);
                        }

当我在调试模式下运行我的应用程序时,一切正常,但是当我从安装程序安装应用程序然后运行时问题就开始了。我猜想会出现某种与许可有关的问题。寻找最佳指导。感谢

修改

问题解决了。我以这种方式在管理模式下以编程方式运行我的应用程序

static bool IsRunAsAdmin()
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(id);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (Environment.OSVersion.Version.Major >= 6)
            {
                if (!IsRunAsAdmin())
                {
                    ProcessStartInfo proc = new ProcessStartInfo();
                    proc.UseShellExecute = true;
                    proc.WorkingDirectory = Environment.CurrentDirectory;
                    proc.FileName = Application.ExecutablePath;
                    proc.Verb = "runas";

                    try
                    {
                        Process.Start(proc);
                    }
                    catch
                    {
                        // The user refused the elevation.
                        // Do nothing and return directly ...
                        return;
                    }

                    Application.Exit();  // Quit itself
                }
                else
                {
                    Application.Run(new frmMain());
                }
            }
            else
            {
                Application.Run(new frmMain());
            }
        }

2 个答案:

答案 0 :(得分:2)

您需要以管理员模式运行应用程序才能为其提供正确的权限。

这里有一些示例代码/说明如何检查您当前所处的模式以及如何提升它:

http://code.msdn.microsoft.com/windowsdesktop/CSUACSelfElevation-644673d3

但是要坚强!在执行此逻辑之前,请确保您在Vista系统或更高版本上运行,否则如果您尝试执行UAC /高程代码,它将破坏早期操作系统的向后兼容性。

您甚至可以使用应用清单自动提升/修改它:

http://msdn.microsoft.com/en-us/library/aa374191(VS.85).aspx

有关使用应用清单方法的更多信息:

http://www.aneef.net/2009/06/29/request-uac-elevation-for-net-application-managed-code/

答案 1 :(得分:2)

安全权限。让您的应用程序在启动时请求管理员访问。

您可以修改清单文件并使用

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
相关问题