Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
System.UnauthorizedAccessException: 'Access to the path 'C:\Users\user\Desktop\New folder (3)' is denied.'

Please, am facing problem in my c# window form application.am try to delete a file after zip the file using using File.delet() method.but it giving an error that System.UnauthorizedAccessException: Access to the path C:\Users\user\Desktop\New folder (3)' is denied.

What I have tried:

C#
public bool CallZiperFolder(List<string> listSelectedFile, string word, string DestinationPath, string ZipName)
        {
            if (listSelectedFile != null)
            {
                Thread thread = new Thread(t =>
                {
                    using (ZipFile zip = new ZipFile())
                {
                    zip.Password = word;
                    // DirectoryInfo directory = new DirectoryInfo(FolderPathToZip);
                    //  zip.Save(Format("{0}{1}.zip", directory.Parent.FullName, directory.Name));
                    foreach (var file in listSelectedFile)
                    {
                            if (YesDo)
                            {
                                zip.AddDirectory(file);
                                DirectoryInfo directory = new DirectoryInfo(file); 
                                File.Delete(file);
                            }
                            else
                            {
                                zip.AddDirectory(file);
                            }
                        }
                    zip.SaveProgress += Zip_SaveProgress;
                    zip.Save(DestinationPath + "\\" + ZipName + ".zip");
                }
            })
                { IsBackground = true };
            thread.Start();
                return true;
            }
            else
                return false;
        }

        private void compressASZipToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox.Text))
            {
                MessageBox.Show("please select a file", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox.Focus();
                return;
            }
            foreach (object item in listBox.Items)
                listSelectedFile.Add(item.ToString());
            DestinationPath = txtBoxdestination.Text;
            ZipName = txtBoxFileName.Text;
            status = CallZiperFolder(listSelectedFile, Password, DestinationPath, ZipName);
            if (status == true)
                MessageBox.Show("Successfully Zipped :", "Successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Some Error Occurred :", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Posted
Updated 24-Aug-21 0:38am
v2

1 solution

It looks like you are trying to delete a directory, not a file, which is not allowed. See File.Delete(String) Method (System.IO) | Microsoft Docs[^] for details.
 
Share this answer
 
Comments
Maciej Los 24-Aug-21 7:05am    
5ed!
Richard MacCutchan 24-Aug-21 7:09am    
Thanks again, Maciej.

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



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