0

i am doing a project that unzips a .zip file to the path "C:\Program Files (x86)". It returns the error :

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in Ionic.Zip.dll

I search a lot but i can´t understand a way to resolve this problem. Help me please .

My code :

namespace UTAD__VPN_w7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void buscarpasta()
        {
            DialogResult result = this.folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {

                /*string pedro = folderBrowserDialog1.SelectedPath;
                string pedro2 = pedro.Replace("\\", "\\\\");
                textBox1.Text = pedro2;*/
                string pasta1 = folderBrowserDialog1.SelectedPath;
                var pasta2 = pasta1.Replace(@"\", @"\\");
                textBox1.Text = pasta2;

            }
        }

        private void MyExtract()
        {


            string zipToUnpack = "C:\\Users\\Zé Eduardo\\Music\\Cisco.zip";
            string unpackDirectory = textBox1.Text;
            using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
            {
                // here, we extract every entry, but we could extract conditionally
                // based on entry name, size, date, checkbox status, etc.  
                foreach (ZipEntry e in zip1)
                {

                    e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
                }
            }
        }





        private void button1_Click(object sender, EventArgs e)
        {
            buscarpasta();




        }
}

I get the path with the folderbrowserdialog1 and then i unzip clicking the button 1

7
  • 1
    The folder "C:\program files (x86)" is protected by the Operating System. You cannot (usually) write there
    – Steve
    Commented May 10, 2013 at 14:21
  • 1
    This is system folder and your application has to be ran with administrator privileges to write to it. Try to unzip to MyDocuments folder for example.
    – Artemix
    Commented May 10, 2013 at 14:21
  • Yes , but how can the other program installers install in that path? I am looking for a code that let my write in that path Commented May 10, 2013 at 14:24
  • 1
    For more than a decade, writing to C:\Program Files has been restricted for non-administrators. Search for [windows] UAC and [windows] privileges here (or on Google without the [] characters, and you'll find thousands of results that explain exactly what the problem is and what needs to be done to work properly with UAC and privileges.
    – Ken White
    Commented May 10, 2013 at 14:26
  • 1
    I think you will need to elevate your credentials.
    – code4life
    Commented May 10, 2013 at 14:32

3 Answers 3

2

Your program probably needs to be run with elevated permissions (e.g., as Administrator). Check out the MSDN documentation for creating a manifest that marks your app as requiring elevation (http://msdn.microsoft.com/en-us/library/windows/desktop/bb756973.aspx). The gist is this:

<requestedExecutionLevel level="requireAdministrator"/>
0

Run your Visual Studio as an Administrator. This is if the application is not intended for external use.

You can also modify your app.config as suggested in the other answer in order to avoid user related issues.

<requestedExecutionLevel level="requireAdministrator"/>
5
  • It will solve my problem? And if i want to run my software in another computer , I am searching for a code that turn on the permissions with code. Commented May 10, 2013 at 14:23
  • @JoséPedroBrito The user on another computer will have to have the administrator's rights to write to that folder as well.
    – Artemix
    Commented May 10, 2013 at 14:24
  • 3
    -1, this suggestion will make the problem worse because it will work for the programmer but not for the user. Commented May 10, 2013 at 14:41
  • 1
    This is almost always a bad idea - it's insecure, and you'll end up with intermediate build files which cannot be cleaned up by your normal account. But if you are testing a program that needs administrative access, you'll need to do something. It might be more proper to compile and then separately run the result as administrator. Running the debugger is probably a bad idea - consider testing the task with another target directory? Commented May 10, 2013 at 14:44
  • @JohnMGant I only suggested that in the context of this being used internally by the programmer. I have updated with an explanation Commented May 14, 2013 at 16:51
0

If the data you are unzipping belongs to your application, a better place to store that data might be in the directory given by

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

It looks like you are dealing with music files, in which case you might want to consider the MyMusic directory instead.

There is a list of the system special folders. Using them will ensure your program will work on different versions of Windows and, if you use the appropriate folder, there will be no problem with filesystem permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.