A
Adam J. Schaff
I am writing a quick program to edit a binary file that contains file paths
(amongst other things). If I look at the files in notepad, they look like:
<gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish>
etc.
I want to remove the "g:\" from the file paths. I wrote a console app that
successfully reads the file and writes a duplicate of it, but fails for some
reason to do the "replacing" of the "g:\". The code follows with a note
showing the line that is not working. When I look at the "s" variable in
break mode, I see that VB does not show the entire file contents, even
though when I write "s" to the second file stream, the entire original file
is duplicated. I suppose this is because the file content isn't intended to
be interpreted as a string (its binary after all). It is probably hitting
some unfriendly bytes that it can't interpret for string operations like
Replace. If that's the case, maybe I need to interact with it a character at
a time, although I'm not sure how I would do a replace that way. Any ideas
or code would be greatly appreciated. As you can tell, I don't do much
binary file I/O.
Sub Main()
'read the binary file into a string var
Dim fs As New FileStream("C:\Source\Sample and
Demo\Foobar\EditFpl\Only Fools.fpl", FileMode.Open, FileAccess.Read)
Dim sr As New StreamReader(fs, System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd
sr.Close()
fs.Close()
'remove the hard-coded drive letter specification
s.Replace("file://G:\", "file://") 'THIS LINE DOES NOT WORK
'write a new binary file with my changes
Dim fs2 As New FileStream("C:\Source\Sample and
Demo\Foobar\EditFpl\Only Fools2.fpl", FileMode.CreateNew, FileAccess.Write)
Dim sw As New StreamWriter(fs2, System.Text.Encoding.UTF8)
sw.Write(s)
sw.Close()
fs2.Close()
End Sub
(amongst other things). If I look at the files in notepad, they look like:
<gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish>
etc.
I want to remove the "g:\" from the file paths. I wrote a console app that
successfully reads the file and writes a duplicate of it, but fails for some
reason to do the "replacing" of the "g:\". The code follows with a note
showing the line that is not working. When I look at the "s" variable in
break mode, I see that VB does not show the entire file contents, even
though when I write "s" to the second file stream, the entire original file
is duplicated. I suppose this is because the file content isn't intended to
be interpreted as a string (its binary after all). It is probably hitting
some unfriendly bytes that it can't interpret for string operations like
Replace. If that's the case, maybe I need to interact with it a character at
a time, although I'm not sure how I would do a replace that way. Any ideas
or code would be greatly appreciated. As you can tell, I don't do much
binary file I/O.
Sub Main()
'read the binary file into a string var
Dim fs As New FileStream("C:\Source\Sample and
Demo\Foobar\EditFpl\Only Fools.fpl", FileMode.Open, FileAccess.Read)
Dim sr As New StreamReader(fs, System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd
sr.Close()
fs.Close()
'remove the hard-coded drive letter specification
s.Replace("file://G:\", "file://") 'THIS LINE DOES NOT WORK
'write a new binary file with my changes
Dim fs2 As New FileStream("C:\Source\Sample and
Demo\Foobar\EditFpl\Only Fools2.fpl", FileMode.CreateNew, FileAccess.Write)
Dim sw As New StreamWriter(fs2, System.Text.Encoding.UTF8)
sw.Write(s)
sw.Close()
fs2.Close()
End Sub