在VB.net零星问题System.IO.Streamwriter它创建该文件,但有一个错误,我不能写行的文件出现后

时间:2019-02-01 21:24:49

标签: vb.net streamwriter

我最近在将StreamWriter偶尔写入文件时遇到了一些问题,希望有人能对此有所了解。我的子程序调用如下:

<!--Code Start
SubPrint(tagVal)
--> End of Code

在我只是用一些随机数填充数组的时候,sub就是这样。 (我稍后将遍历真实数据并以这种方式填充它):

<!--Code Start
Public Sub SubPrint(item As String)
    Dim sw As StreamWriter =
        New StreamWriter("C:\Users\dlawrence\Desktop\Fred.txt", False)
    Dim tags() As Integer = {1, 3, 6, 2, 6, 2}
    Dim max As Integer = tags(0)
    For i = 1 To tags.Count - 1
        If tags(i) > max Then
            max = tags(i)
        End If
             sw.WriteLine(tags(i).ToString) 'EDIT moved this out of the If
    Next


End Sub
--> End of Code

它会在运行时在我的桌面上创建文件“ Fred.txt”,但会引发异常错误:

<!--Error Log Start
System.IO.IOException
  HResult=0x80070020
  Message=The process cannot access the file 'C:\Users\dlawrence\Desktop\Fred.txt' because it is being used by another process.
  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.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append)
   at Tagging_App_GUI.Form1.SubPrint(String item) in C:\Users\dlawrence\Documents\iLogicIdeasForPunchedWindows\TagAddIn\New AddIn\NewGUI\Tagging App GUI\Tagging App GUI\Form1.vb:line 184
   at Tagging_App_GUI.Form1.BtnTag_Click(Object sender, EventArgs e) in C:\Users\dlawrence\Documents\iLogicIdeasForPunchedWindows\TagAddIn\New AddIn\NewGUI\Tagging App GUI\Tagging App GUI\Form1.vb:line 150
   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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() in f:\dd\vb\runtime\msvbalib\ApplicationServices\WindowsFormsApplicationBase.vb:line 779
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() in f:\dd\vb\runtime\msvbalib\ApplicationServices\WindowsFormsApplicationBase.vb:line 1471
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) in f:\dd\vb\runtime\msvbalib\ApplicationServices\WindowsFormsApplicationBase.vb:line 452
   at Tagging_App_GUI.My.MyApplication.Main(String[] Args) in :line 81
--> End of Error logging

前几天我很幸运在另一部分代码中对此进行了编写,但今天没有。我现在用的是进口的语句通过生产没有喜悦系统的进口System.IO重新启动才能获得系统和IO命名空间两者。有一个内部的异常,其值是没有什么...

有人能看到我没看见的东西吗?谢谢!

1 个答案:

答案 0 :(得分:1)

问题是您不是closing的{​​{1}},因此每次尝试再次写入它时,它都在使用中,因此StreamWriter

Error行之后添加sw.Close,如下所示:

Next

或更妙的是,在使用 Dim sw As IO.StreamWriter = New IO.StreamWriter("C:\Users\dlawrence\Desktop\Fred.txt") Dim tags() As Integer = {1, 3, 6, 2, 6, 2} Dim max As Integer = tags(0) For i = 1 To tags.Count - 1 If tags(i) > max Then max = tags(i) End If sw.WriteLine(tags(i).ToString) 'EDIT moved this out of the If Next sw.Close() 时使用Using块,如下所示:

StreamWriter