另一个程序msg使用的文件 [英] file use by another program msg
本文介绍了另一个程序msg使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
方法在这里,
文件名提取已成功解压缩,但是当我转到目录&时,尝试删除它,但是它说它正在被另一个程序使用,
我错过了什么?
The method is here,
The file name extract which is unziped successfully, but when i goto the directory & try to delete it,but it says it is using by another program,
what i got missing?
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块内,例如
Use File.Close() or enclose the code inside the using blocks like this
using(ZipInputStream stream = new ZipInputStream(File.OpenRead(@"c:\DoUnZip.zip"));){
// do your work here
}
尝试一下我在应用程序中使用过的代码,它可以正常工作:-
Try this code what i have used in my application and it is working fine :-
public string Zip(string ZipFileName)
{
try
{
if (!File.Exists(ZipFileName))
{
//lstProcess.Items.Add("File Does Not Exists.");
return "";
}
using (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);
// create directory
if (directoryName.Length > 0)
{
Directory.CreateDirectory(Server.MapPath("") + "\\XML\\" + directoryName);
}
if (fileName != String.Empty)
{
using (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 (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
return fileName;
}
}
catch (Exception ex)
{
//Display Error
return "";
}
finally
{
//Update Status
}
}
调用此功能
------------------------------
字符串extractFileName = Server.MapPath(")+"\\ XML \\" + Zip(FilePath);
calling to this function
------------------------------
string extractFileName = Server.MapPath("") + "\\XML\\" + Zip(FilePath);
这篇关于另一个程序msg使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文