Solved

Convert C# code for concatenating files to VB

Posted on 2004-08-11
6
226 Views
Last Modified: 2012-06-27
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.Text);
FileStream file2  = File.OpenRead(textBox2.Text);
FileStream output = File.OpenWrite("output.file");

int fileByte;
while((fileByte = file1.ReadByte()) != -1)
{
     output.WriteByte((byte)fileByte);              
}

while((fileByte = file2.ReadByte()) != -1)
{
     output.WriteByte((byte)fileByte);              
}

file1.Close();
file2.Close();
output.Close();

=================================
COVERTED VB CODE
=================================
        Dim file1 As New FileStream("c:\temp\ini_files\ini_part_1.txt", FileMode.Open)
        Dim file2 As New FileStream("c:\temp\ini_files\ini_part_2.txt", FileMode.Open)
        Dim fileOutput As New FileStream("c:\temp\ini_files\ini_part_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
0
Comment
Question by:GCaron
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
Clif earned 150 total points
ID: 11773648
How about:

fileOutput.WriteByte(CType(fileByte, Byte))
0
 
LVL 7

Assisted Solution

by:mdamico
mdamico earned 100 total points
ID: 11773821
In case you aren't aware of these, there are a couple online sites that do C# to VB.Net conversions.  

http://www.developerfusion.com/utilities/convertcsharptovb.aspx

http://authors.aspalliance.com/aldotnet/examples/translate.aspx
0
 

Author Comment

by:GCaron
ID: 11774275
I split the points as you both answered my main question although I am still generating a continous file.

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_files\ini_part_1.txt")
        Dim file2 As FileStream = file.OpenRead("c:\temp\ini_files\ini_part_2.txt")
        Dim fileOutput As FileStream = file.OpenWrite("c:\temp\ini_files\ini_part_3.txt")
        Dim fileByte As Integer

        While Not ((fileByte = file1.ReadByte()) = -1)
            fileOutput.WriteByte(CType(fileByte, Byte))
        End While

        While Not ((fileByte = file2.ReadByte()) = -1)
            fileOutput.WriteByte(CType(fileByte, Byte))
        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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:Clif
ID: 11774394
Go back to using:

        Dim file1 As New FileStream("c:\temp\ini_files\ini_part_1.txt", FileMode.Open)
        Dim file2 As New FileStream("c:\temp\ini_files\ini_part_2.txt", FileMode.Open)
        Dim fileOutput As New FileStream("c:\temp\ini_files\ini_part_3.txt", FileMode.CreateNew)


0
 

Author Comment

by:GCaron
ID: 11774984
Thanks, the final code I used to achieve what I needed is as follows;

===============================================

        Dim fn1 As String = "c:\temp\ini_files\ini_part_1.txt"
        Dim fn2 As String = "c:\temp\ini_files\ini_part_2.txt"
        Dim fn3 As String = "c:\temp\ini_files\ini_part_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
0
 
LVL 10

Expert Comment

by:Clif
ID: 11775072
Glad to be of service.  :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question