You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2019 VB.net文件占用异常0x80070020技术求助

问题求助:WinForm程序修改代码后访问共享文件触发占用异常

背景

  • 刚启用全新Windows 10电脑
  • 运行与旧电脑完全相同版本的VS2019
  • 项目为含多复选框、文本框的WinForm程序,用户操作后通过StreamWriter将控件状态写入共享网络的.enc格式文本文件;再次打开时用StreamReader读取文件恢复控件状态
  • 网络管理端显示该文件未被任何用户占用

此程序已稳定运行多年,IT架构无变动,仅我的新电脑出现问题。

问题

  • 打开现有项目不修改代码,通过Debug/Release运行或直接执行EXE均正常
  • 只要修改任何代码(哪怕仅添加注释),就会触发文末的异常
  • 从代码层面看,异常绝非流释放问题(见下文尝试方案A)
  • 本地Windows资源管理器修改/删除该文件时提示“文件正在使用”,其他电脑可正常删除
  • 首次运行程序创建文件无问题,再次运行加载该文件时崩溃,文件显示被占用

已尝试方案

  • A. 两种流释放方式:Stream.Close()及Using...End Using(见下方代码片段)
  • B. 同事电脑运行相同源码及发布后的EXE均无问题
  • C. 用ProcessMonitor、ProcessExplorer排查关联进程/DLL,未找到相关记录
  • D. 禁用杀毒软件
  • E. 其他电脑运行正常
  • F. 同事编译的EXE在我电脑运行仍报错
  • G. 修复并重装VS2019
  • H. 安全模式运行

代码片段

方法1

Dim filewriter As System.IO.StreamWriter

filewriter = My.Computer.FileSystem.OpenTextFileWriter(dirNewENC & newfile, True)
filewriter.WriteLine("shortcutCreated=" & shortcutCreated)
filewriter.WriteLine("shortcutPath=" & shortcutPath)
...
filewriter.Close()

方法2

Using filewriter = My.Computer.FileSystem.OpenTextFileWriter(dirNewENC & newfile, True)
filewriter.WriteLine("shortcutCreated=" & shortcutCreated)
filewriter.WriteLine("shortcutPath=" & shortcutPath)
...
End Using

异常详情

System.IO.IOException
HResult=0x80070020
Message=无法访问文件 '\affs01\Shared\Sales\Quotes\ENC\TEST8%01%TEST%Q99999998%44456%.enc',因为它正被另一个进程使用。
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks)
at Microsoft.VisualBasic.FileIO.FileSystem.OpenTextFileReader(String file, Encoding encoding)
at Microsoft.VisualBasic.MyServices.FileSystemProxy.OpenTextFileReader(String file)
at Order_Entry.GlobalSubs.checkOrderType() in C:\Users\RB\OneDrive\Code\repos\Order Entry\Order Entry\GlobalSubs.vb:line 6348
at Order_Entry.frm_NewEntry.btn_searchQuote_Click(Object sender, EventArgs e) in C:\Users\RB\OneDrive\Code\repos\Order Entry\Order Entry\frm_NewEntry.vb:line 152
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.PerformClick()
at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Order_Entry.My.MyApplication.Main(String[] Args) in :line 83

内容的提问来源于stack exchange,提问作者buerm009

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 18:15:41