I want to delete the text file output.txt ,but I get error message. File cannot be deleted used by another process. Can you please help me..I tried close nad flush commands for filestream and stream readers..but did not help..
Here is my code.
Dim chkstr As String
chkstr = ("usb_download -crc -device VID_" + VIDnum + "_PID_" + PIDnum + " >> output.txt")
Dim proc5 As New System.Diagnostics.Process() 'VID/PID query'
proc5.StartInfo.FileName = "CMD.exe" '"usb_download.exe"
' proc5.StartInfo.Arguments = chkstr
proc5.StartInfo.WorkingDirectory = "C:\downloadtest\"
' proc5.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
proc5.StartInfo.RedirectStandardOutput = True
proc5.StartInfo.RedirectStandardInput = True
proc5.StartInfo.UseShellExecute = False
proc5.StartInfo.CreateNoWindow = True
proc5.Start()
Dim Sw As IO.StreamWriter = proc5.StandardInput
Sw.WriteLine(chkstr)
' Sr.ReadLine()
' Dim str As String = Sr.ReadToEnd
proc5.WaitForExit(5000)
proc5.Close()
Dim fs As New FileStream("C:\downloadtest\output.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, FileShare.Delete)
Dim Sr As New IO.StreamReader(fs)
Dim strtext As String
strtext = Sr.ReadToEnd
statustext.Text = strtext
fs.flush()
fs.close()
Sr.close
Sw.close
If System.IO.File.Exists("C:\downloadtest\output.txt") = True Then
System.IO.File.Delete("C:\downloadtest\output.txt")
End If
End Sub
Thanks in advance for help.