我正在尝试在 zip 文件中写入 xml
static void Main(string[] args)
{
String strPathofZip = @"C:/Work/Zip/PATHDATA DT2.zip";
ZipFile zipFile = new ZipFile(strPathofZip);
zipFile.BeginUpdate();
foreach (ZipEntry ze in zipFile)
{
if (ze.Name == "SOURCEDATACONFIG.XML")
{
StreamReader s = new StreamReader(zipFile.GetInputStream(ze));
{
XmlDocument XDoc = new XmlDocument();
XDoc.Load(s);
XmlNodeList NodeList = XDoc.SelectNodes(@"R/I");
foreach (XmlNode Node in NodeList)
{
XmlElement Elem = (XmlElement)Node;
Elem.SetAttribute("url", "77");
//Elem.SetAttribute("url", "3");
}
XDoc.Save("SOURCEDATACONFIG.XML");
}
zipFile.Add(ze.Name);
}
}
Console.WriteLine(zipFile.Name);
zipFile.CommitUpdate();
Console.ReadLine();
}
代码在控制台应用程序中运行,并且 Xml 已更新,但
如果我尝试使用与 Windows 窗体中的函数相同的代码......它会引发错误
private void settingAttributeinZipppedXML(string ZipPath)
{
ZipFile zipFile = new ZipFile(ZipPath);
zipFile.BeginUpdate();
foreach (ZipEntry ze in zipFile)
{
if (ze.Name == "SOURCEDATACONFIG.XML")
{
using (StreamReader s = new StreamReader(zipFile.GetInputStream(ze)))
{
XmlDocument XDoc = new XmlDocument();
XDoc.Load(s);
XmlNodeList NodeList = XDoc.SelectNodes(@"R/I");
foreach (XmlNode Node in NodeList)
{
XmlElement Elem = (XmlElement)Node;
Elem.SetAttribute("url", "12");
}
XDoc.Save("SOURCEDATACONFIG.XML");
}
zipFile.Add(ze.Name);
}
}
zipFile.CommitUpdate();
}
错误是
找不到文件“C:\Work\Zip\PATHDATA DT2.zip.561.tmp”。
还有一件事:如果我调试代码一段时间,程序就会运行。
什么会导致这种行为?
堆栈跟踪在这里..
堆栈跟踪: 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.File.Move(字符串源文件名,字符串 destFileName) 在 ICSharpCode.SharpZipLib.Zip.DiskArchiveStorage.ConvertTemporaryToFinal() 在 ICSharpCode.SharpZipLib.Zip.ZipFile.RunUpdates() 在 ICSharpCode.SharpZipLib.Zip.ZipFile.CommitUpdate() 在 SDEProfilePathEditor.SDEProfilePathEditorForm.settingAttributeinZipppedXML(String ZipPath) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 SDEProfilePathEditor.Program.Main() 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常: