代码没有运行,因为另一个线程正在访问它

时间:2013-10-11 13:02:46

标签: c#

我正在尝试在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'。

还有一件事:如果我调试代码一段时间,程序就会运行。

什么会导致这种行为?

Stack Trace就在这里..

StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.File.Move(String sourceFileName, String destFileName)
       at ICSharpCode.SharpZipLib.Zip.DiskArchiveStorage.ConvertTemporaryToFinal()
       at ICSharpCode.SharpZipLib.Zip.ZipFile.RunUpdates()
       at ICSharpCode.SharpZipLib.Zip.ZipFile.CommitUpdate()
       at SDEProfilePathEditor.SDEProfilePathEditorForm.settingAttributeinZipppedXML(String ZipPath)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at SDEProfilePathEditor.Program.Main()
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

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

如果您希望使用Console.WriteLine使用Windows窗体应用程序在控制台上显示输出添加此属性并从主窗体构造函数调用它。然后它也可以通过控制台打开。

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

  [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  private static extern bool AllocConsole();

答案 2 :(得分:0)

问题是其他一些Thread打开了那个zip。比我的功能是创建一个临时zip,这就是创建问题。

zipFile.Close();

添加了

以关闭已打开的zip文件。

谢谢大家的意见。