簡體   English   中英

“訪問路徑被拒絕”問題

[英]“Access to path denied” issue

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文件中保存任何值時,我收到錯誤。 錯誤屏幕截圖在這里。 在此輸入圖像描述

現在我想展示我如何在運行時寫入數據進行配置

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());
            }
        }

您需要以管理員模式運行應用程序才能為其提供正確的權限。

這里有一些示例代碼/說明如何檢查您當前所處的模式以及如何提升它:

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/

安全權限。 讓您的應用程序在啟動時請求管理員訪問。

您可以修改清單文件並使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM