Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all , 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 :

C#
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
Posted

You cannot under normal circumstances have write access to the Program Files folder on OS's from Vista on up. The idea is that it reduces the incidence of virus activity, since you should not need to write there.

So you basically have two options:
1) Upgrade your application access to Admin (which will mean the user giving his permission in the same way that he has to for installations, and for the same reason)
2) Find somewhere better to extract your files. If they are data files, then they really, really shouldn't be under the Program Files folder at all - access restrictions here will only get stronger, not weaker so you will have more problems in the future, not less. For data file, consider moving them here: Where should I store my data?[^]
 
Share this answer
 
check that you have rights for that directory
 
Share this answer
 
Users don't have Write permissions to Program Files. It's that way for a reason. You shouldn't be writing anything to any folders under Program Files in your application. The data should be written to more accessible and appropriate locations. Check the Environment.SpecialFolder enum[^] documentation for a list of those folders and what they are intended for.
 
Share this answer
 
Older windows versions have no security implications that prevent overwriting program files. Nowadays there are some restrictions and one of it is that a normal user can't overwrite program files, and therefor applications started by the user also can't. You need to run as administrator or write it to the folder of the user. You can find it using this:
C#
string applicationDataPath=
   Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);


You could also add a manifest, requesting administrator privileges:
http://msdn.microsoft.com/en-us/library/bb756929.aspx[^]

Good luck!
 
Share this answer
 
v2
I think that for default microsoft windows don´t five rights to that directory , i want to turn on permissions to that path with code in C# , i search a lot and i found some codes, but i can´t implement them.
 
Share this answer
 
Ok , i understand you all , so how can the installers like Ccleaner and other install their software to that path?
 
Share this answer
 
Comments
Ron Beyer 10-May-13 10:42am    
Installers usually require or request administrative privileges for installation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900