I am working on a WPF project on .NET framework so I cant use the latest assembly of System.IO.ZipFile`. The problem seems trivial but it's taking my day away.
I have this task:
public async Task SetupEnvironment(IProgressReporter progressReporter)
{
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string pythonExePath = System.IO.Path.Combine(basePath, @"CPython\python-embed\python.exe");
if (!File.Exists(pythonExePath))
{
progressReporter.ReportProgress($"Python environment not found. Setting up environment in {basePath}");
Thread.Sleep(300);
string zipFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"CPython\python.zip");
//string extractPath = System.IO.Path.Combine(basePath, @"CPython");
string extractPath = @"C:\Data\python";
try
{
progressReporter.ReportProgress($"exctraxcting environment ");
//ZipFile.ExtractToDirectory(zipFilePath, extractPath);
using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (progressReporter != null)
{
progressReporter.ReportProgress($"exctraxcting {entry.Name}");
}
entry.ExtractToFile(System.IO.Path.Combine(extractPath, entry.Name), overwrite: true);
}
}
progressReporter.ReportProgress($"Python environment set up successfully.");
Thread.Sleep(5000);
}
catch (Exception ex)
{
progressReporter.ReportProgress($"Error setting up Python environment: {ex.Message}");
MessageBox.Show(ex.ToString());
Thread.Sleep(5000);
}
}
else
{
progressReporter.ReportProgress($"Found existing Python environment in {basePath}");
Thread.Sleep(5000);
}
}
The heart of this function is
entry.ExtractToFile(System.IO.Path.Combine(extractPath, entry.Name), overwrite: true);
Regardless which path I use for the output I get the same error on this line of code:
await Task.Run(() => { SetupEnvironment(this); });
The process cannot access the file
C:\Data\python\_asyncio.pydbecause it is being used by another process. Obviously _asyncio.pyd is part of the archive I am trying to extract
and also
System.UnauthorizedAccessException: Access to the path 'C:\Data\python' is denied.
Googling I found this thread
A process cannot access a File because it is being used by another process
but also trying to change the destination path (with one not included in the source path (as I do in the example)) didn't help
The problem was that initially some
entry.namewere empty in the loop. Theextractmethod was crashing trying to access a busy object. Here is the updated function: