文件被其他程序使用msg[英] file use by another program msg

本文是小编为大家收集整理的关于文件被其他程序使用msg的处理方法,想解了文件被其他程序使用msg的问题怎么解决?文件被其他程序使用msg问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

方法在这里,
文件extract 已成功解压缩,但是当我转到目录 &尝试删除它,但它说它正在被另一个程序使用,
我错过了什么?

public static void UnZipFiles()
   {
       ZipInputStream stream = new ZipInputStream(File.OpenRead(@"c:\DoUnZip.zip"));
       ZipEntry theEntry;
       FileStream streamWriter = null;
       try
       {
           string tmpEntry = String.Empty;
           while ((theEntry = stream.GetNextEntry()) != null)
           {
               string directoryName = @"d:\extract";
               string fileName = Path.GetFileName(theEntry.Name);
               // create directory
               if (directoryName != "")
               {
               System.IO.Directory.CreateDirectory(directoryName);
               }
               if (fileName != String.Empty)
               {
                   if (theEntry.Name.IndexOf(".ini") < 0)
                   {
                       string fullPath = directoryName + "\\" + theEntry.Name;
                       fullPath = fullPath.Replace("\\ ", "\\");
                       string fullDirPath = Path.GetDirectoryName(fullPath);
                       if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                        streamWriter = File.Create(fullPath);
                       int size = 2048;
                       byte[] data = new byte[size];
                       while (true)
                       {
                           size = stream.Read(data, 0, data.Length);
                           if (size > 0)
                           {
                               streamWriter.Write(data, 0, size);
                           }
                           else
                           {
                               break;
                           }
                       }
                   }
               }
           }
       }

推荐答案

使用 File.Close() 或者像这样将代码包含在 using 块中

使用(ZipInputStream stream = new ZipInputStream(File.OpenRead(@"c:\DoUnZip.zip"));){//在这里做你的工作}

试试我在我的应用程序中使用过的代码,它工作正常:-

public string Zip(字符串 ZipFileName){试试{if (!File.Exists(ZipFileName)){//lstProcess.Items.Add("文件不存在.");返回 "";}使用 (ZipInputStream s = new ZipInputStream(File.OpenRead(ZipFileName))){string fileName = "";ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(theEntry.Name);fileName = Path.GetFileName(theEntry.Name);//创建目录if (directoryName.Length > 0){Directory.CreateDirectory(Server.MapPath("") + "\\XML\\" + directoryName);}if (fileName != String.Empty){使用 (FileStream streamWriter = File.Create(Server.MapPath("") + "\\XML\\" + theEntry.Name)){int size = 2048;byte[] data = new byte[2048];while (true){size = s.Read(data, 0, data.Length);if (尺寸 > 0){streamWriter.Write(data, 0, size);}其他{break;}}}}}return 文件名;}}catch(例外){//显示错误返回 "";}终于{//更新状态}}



调用这个函数
------------------------------
string extractFileName = Server.MapPath("") + "\\XML\\" + Zip(FilePath);

本文地址:https://www.itbaoku.cn/post/1355742.html