GCaron
asked on
Convert C# code for concatenating files to VB
VB.NET newbie question.....
I am trying to convert the following C# code from a similar Q&A to VB to combine multiple files together but ended up creating a large continuously growing file.
========================== =======
ORIGINAL C# CODE
========================== =======
FileStream file1 = File.OpenRead(textBox1.Tex t);
FileStream file2 = File.OpenRead(textBox2.Tex t);
FileStream output = File.OpenWrite("output.fil e");
int fileByte;
while((fileByte = file1.ReadByte()) != -1)
{
output.WriteByte((byte)fil eByte);
}
while((fileByte = file2.ReadByte()) != -1)
{
output.WriteByte((byte)fil eByte);
}
file1.Close();
file2.Close();
output.Close();
========================== =======
COVERTED VB CODE
========================== =======
Dim file1 As New FileStream("c:\temp\ini_fi les\ini_pa rt_1.txt", FileMode.Open)
Dim file2 As New FileStream("c:\temp\ini_fi les\ini_pa rt_2.txt", FileMode.Open)
Dim fileOutput As New FileStream("c:\temp\ini_fi les\ini_pa rt_3.txt", FileMode.CreateNew)
Dim fileByte As Integer
While ((fileByte = file1.ReadByte()) <> -1)
fileOutput.WriteByte((byte )fileByte)
End While
While ((fileByte = file2.ReadByte()) <> -1)
fileOutput.WriteByte((byte )fileByte)
End While
file1.Close()
file2.Close()
fileOutput.Close()
========================== =======
I'm mainly unsure how to covert the line:
fileOutput.WriteByte((byte )fileByte)
I received "'byte' is a type, and so is not a valid expression" from Visual Studio.
Detailed code or general pointers would be most appreciated.
Thank you
I am trying to convert the following C# code from a similar Q&A to VB to combine multiple files together but ended up creating a large continuously growing file.
==========================
ORIGINAL C# CODE
==========================
FileStream file1 = File.OpenRead(textBox1.Tex
FileStream file2 = File.OpenRead(textBox2.Tex
FileStream output = File.OpenWrite("output.fil
int fileByte;
while((fileByte = file1.ReadByte()) != -1)
{
output.WriteByte((byte)fil
}
while((fileByte = file2.ReadByte()) != -1)
{
output.WriteByte((byte)fil
}
file1.Close();
file2.Close();
output.Close();
==========================
COVERTED VB CODE
==========================
Dim file1 As New FileStream("c:\temp\ini_fi
Dim file2 As New FileStream("c:\temp\ini_fi
Dim fileOutput As New FileStream("c:\temp\ini_fi
Dim fileByte As Integer
While ((fileByte = file1.ReadByte()) <> -1)
fileOutput.WriteByte((byte
End While
While ((fileByte = file2.ReadByte()) <> -1)
fileOutput.WriteByte((byte
End While
file1.Close()
file2.Close()
fileOutput.Close()
==========================
I'm mainly unsure how to covert the line:
fileOutput.WriteByte((byte
I received "'byte' is a type, and so is not a valid expression" from Visual Studio.
Detailed code or general pointers would be most appreciated.
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Go back to using:
Dim file1 As New FileStream("c:\temp\ini_fi les\ini_pa rt_1.txt", FileMode.Open)
Dim file2 As New FileStream("c:\temp\ini_fi les\ini_pa rt_2.txt", FileMode.Open)
Dim fileOutput As New FileStream("c:\temp\ini_fi les\ini_pa rt_3.txt", FileMode.CreateNew)
Dim file1 As New FileStream("c:\temp\ini_fi
Dim file2 As New FileStream("c:\temp\ini_fi
Dim fileOutput As New FileStream("c:\temp\ini_fi
ASKER
Thanks, the final code I used to achieve what I needed is as follows;
========================== ========== ========== =
Dim fn1 As String = "c:\temp\ini_files\ini_par t_1.txt"
Dim fn2 As String = "c:\temp\ini_files\ini_par t_2.txt"
Dim fn3 As String = "c:\temp\ini_files\ini_par t_3.txt"
Dim f1 As New FileStream(fn1, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim f2 As New FileStream(fn2, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim f3 As New FileStream(fn3, FileMode.Create, FileAccess.Write, FileShare.Read)
' Create a reader that can read bytes from the FileStream.
Dim sr1 As New BinaryReader(f1)
' While not at the end of the file, read lines from the file.
While sr1.PeekChar() > -1
Dim input As Byte = sr1.ReadByte()
f3.WriteByte(CType(input, Byte))
End While
sr1.Close()
' Create a reader that can read bytes from the FileStream.
Dim sr2 As New BinaryReader(f2)
' While not at the end of the file, read lines from the file.
While sr2.PeekChar() > -1
Dim input As Byte = sr2.ReadByte()
f3.WriteByte(CType(input, Byte))
End While
sr2.Close()
f1.Close()
f2.Close()
f3.Close()
========================== ========== ========== =
Thanks again
==========================
Dim fn1 As String = "c:\temp\ini_files\ini_par
Dim fn2 As String = "c:\temp\ini_files\ini_par
Dim fn3 As String = "c:\temp\ini_files\ini_par
Dim f1 As New FileStream(fn1, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim f2 As New FileStream(fn2, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim f3 As New FileStream(fn3, FileMode.Create, FileAccess.Write, FileShare.Read)
' Create a reader that can read bytes from the FileStream.
Dim sr1 As New BinaryReader(f1)
' While not at the end of the file, read lines from the file.
While sr1.PeekChar() > -1
Dim input As Byte = sr1.ReadByte()
f3.WriteByte(CType(input, Byte))
End While
sr1.Close()
' Create a reader that can read bytes from the FileStream.
Dim sr2 As New BinaryReader(f2)
' While not at the end of the file, read lines from the file.
While sr2.PeekChar() > -1
Dim input As Byte = sr2.ReadByte()
f3.WriteByte(CType(input, Byte))
End While
sr2.Close()
f1.Close()
f2.Close()
f3.Close()
==========================
Thanks again
Glad to be of service. :)
ASKER
I am now using the following code after changing some lines due to the suggestions and conversions:
==========================
Dim file1 As FileStream = file.OpenRead("c:\temp\ini
Dim file2 As FileStream = file.OpenRead("c:\temp\ini
Dim fileOutput As FileStream = file.OpenWrite("c:\temp\in
Dim fileByte As Integer
While Not ((fileByte = file1.ReadByte()) = -1)
fileOutput.WriteByte(CType
End While
While Not ((fileByte = file2.ReadByte()) = -1)
fileOutput.WriteByte(CType
End While
file1.Close()
file2.Close()
fileOutput.Close()
==========================
I am now receiving the error alert on the lines referencing "file.OpenRead" stating "Local variable 'file' cannot be referred to before it is declared". I know I am close but I just don't see what I am missing?
Thanks again