3

我正在尝试在 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()
  内部异常:
4

3 回答 3

0

试试这个确保你在可执行文件运行的地方使用 currentDirectory()

private void settingAttributeinZipppedXML(string ZipPath)
{
ZipFile zipFile = new ZipFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\PATHDATA DT2.zip");
    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();
}
于 2013-10-11T14:20:17.157 回答
0

Console.WriteLine仅在控制台应用程序中工作。所以不显示任何输出,似乎您的应用程序是 Windows 窗体应用程序。

如果您想使用Console.WriteLinewindows 窗体应用程序在控制台上显示输出,只需添加此属性并从主窗体构造函数中调用它。然后它也可以使用控制台打开。

 public InsertNames()
 {
   AllocConsole();
   InitializeComponent();
 }

  [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  private static extern bool AllocConsole();
于 2013-10-14T07:36:41.780 回答
0

问题是其他一些线程已经打开了那个 zip。比我的功能是创建一个临时 zip 并创建问题。

zipFile.Close();

添加以关闭已打开的 zip 文件。

谢谢大家的建议。

于 2013-10-14T08:23:06.460 回答