在其中创建.txt文件后,无法将%temp%中的文件夹压缩为.zip文件-“文件正在由另一个进程使用”

时间:2018-12-22 15:37:55

标签: c# windows

我正在用C#为Windows创建一个应用程序,该程序将.zip文件提取到%temp%文件夹中,并向您显示内容并进行编辑。完成文件编辑后,您可以再次保存文件-该应用程序将删除原始.zip文件,并将该文件夹压缩为相同的文件(已删除),使其看起来像已被替换。

我尝试通过该应用程序创建一个新的空.zip文件并保存。那行得通。然后,我还尝试在其中创建一个文件夹并保存它。那也可以,但是当我创建一个新的.txt文件时,它给了我“正在使用文件”的错误。当我编辑.zip存档中的.txt文件并将其保存时,不会发生此错误。

//.txt file creation into the dir where the .zip file was extracted
System.IO.File.Create(System.IO.Path.GetTempPath() + "UltraDocs\\" + rnd + "\\" + listBox1.GetItemText(listBox1.SelectedItem) + "\\" + toolStripTextBox3.Text + ".txt");
System.IO.File.SetAttributes(System.IO.Path.GetTempPath() + "UltraDocs\\" + rnd + "\\" + listBox1.GetItemText(listBox1.SelectedItem) + "\\" + toolStripTextBox3.Text + ".txt", ~System.IO.FileAttributes.ReadOnly);
//Compression
System.IO.File.Delete(filedia.FileName);
ZipFile.CreateFromDirectory(System.IO.Path.GetTempPath() + "UltraDocs\\" + rnd, filedia.FileName);
//This line gives me a "*.txt is currently in use by another process" error

有什么想法可能导致此错误,以及如何解决该错误?

0 个答案:

没有答案