未作为另一个线程运行的代码正在访问它

本文关键字:代码 访问 运行 另一个 线程 | 更新日期: 2023-09-27 18:34:50

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

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

什么会导致此行为?

堆栈跟踪在这里..

堆栈跟踪:       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, MouseButton 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((       在System.Threading.ThreadHelper.ThreadStart_Context(对象状态(       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((  内部异常:

未作为另一个线程运行的代码正在访问它

试试这个 确保你使用 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();
}

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

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

 public InsertNames()
 {
   AllocConsole();
   InitializeComponent();
 }
  [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  private static extern bool AllocConsole();

问题是其他线程打开了该zip。比我的函数正在创建一个临时zip,这造成了问题。

zipFile.Close();

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

谢谢大家的建议。