В последнее время у меня были некоторые проблемы с записью StreamWriter в файл время от времени, и, надеюсь, кто-то может попытаться пролить свет на него.Мой вызов подпрограммы следующий:
<!--Code Start
SubPrint(tagVal)
--> End of Code
И подпрограмма просто такая в тот момент, когда я просто заполняю массив некоторыми случайными числами.(Позже я буду перебирать реальные данные и заполнять их таким образом):
<!--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
Мне повезло, когда я писал об этом на днях в другом разделе кода, но не сегодня.Я использую оператор Imports для получения пространства имен системы и ввода-вывода через Imports System.IO. Перезапуск системы не принес радости.Было внутреннее исключение, значение которого было Nothing ...
Кто-нибудь может увидеть то, чего я не вижу?Спасибо!