Solved

How to change a specific bit from a file

Posted on 2016-11-09
2
29 Views
Last Modified: 2016-11-10
Hi.

Anyone know how to read a file (binary file) and change a specific bit and then write the file back in vb.net?

I'm trying to change mp3 header bit. The samples I've seen only show with text file.
0
Comment
Question by:MCSI Developers
2 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41881182
Hi MCSI Developers;

The following will show you how to modify a file in binary mode.
Imports System.IO

'' File to be opened
Dim filename = "mpthreetest.mp3"
'' Set up array to read the bytes from the file system into memory
Dim readFromFile() As Byte
'' Used to hold the file size
Dim filesize As Long

'' Read the file into the array the FileStream
Using fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    '' Reading the file with a binary reader
    Using br As New BinaryReader(fs)
        '' Get the size of the file
        filesize = br.BaseStream.Length - 1
        '' Create the array with the file size elements
        readFromFile = New Byte(filesize) {}
        '' Make sure the we are at the begining of the file
        br.BaseStream.Seek(0, SeekOrigin.Begin)
        '' Read all bytes into array
        br.Read(readFromFile, 0, filesize)
    End Using
End Using

'' Modify the array at the position needed
'' this statement will change the value of the 9th byte of the file 
'' to a decimal 9. Note I am assigning the value in hex
readFromFile(8) = &H39

'' After modifyinf the array write all bytes back to the file
Using fs As New FileStream(filename, FileMode.Open, FileAccess.Write)
    Using bw As New BinaryWriter(fs)
        bw.Write(readFromFile)
    End Using
End Using

Open in new window

1
 
LVL 1

Author Comment

by:MCSI Developers
ID: 41882128
Thanks, I've used VB5.0 to do this.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide Tab Page 3 33
Spacing between controls 4 29
Get Top Visible Row of Datagridview 6 26
VB.NET return row from SQL to DataSet and get column values ? 3 24
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now