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