Thomas
Thomas

Reputation: 34188

"Access to path denied" issue

1) i was trying to create directory programmatically this way

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

the above code works fine when i run my apps in debug mode but after creating setup and when i installed then when the above line execute and try to create folder in my apps installation location then problem start. what would be the best approach to get rid of this prob because i will distribute this setup to 3rd part and they can install my apps any where which i may not know.

2) when i try to save any value in app.config file then i am getting error. the error screen shot is here. enter image description here

now i like to show how i am write data to config at runtime

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

everything is working fine when i run my apps in debug mode but problem start when i install the apps from setup and then run. i guess some kind of permission related problem occur. looking for best guidance. thanks

EDIT

Problem solved. i run my apps programmatically in admin mode this way

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

Upvotes: 3

Views: 1842

Answers (2)

Tsukasa
Tsukasa

Reputation: 6552

Security permissions. Let your application request administrator access when it's launched.

You can modify your manifest file and use

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

Upvotes: 2

Ahmed ilyas
Ahmed ilyas

Reputation: 5822

you need to probably run the app in administrator mode to give it the right permissions.

here is some sample code/explanation how to check to see what mode you are currently in and how to elevate it:

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

however be robust! make sure you are running on a Vista system or higher before doing this logic otherwise it will break backward compatibility for earlier OS's if you try to execute the UAC/elevation code.

you may even be able to auto elevate using the app manifest/modifying it:

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

more information about using the app manifest approach:

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

Upvotes: 2

Related Questions