How to read file into a byte array in C#?

How would you read the following file into a byte array after it has been completely written to in a loop statement?

string bankFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");
            using (FileStream fs = new FileStream(bankFilePath, FileMode.Create))
            {
                using (BinaryWriter bwrite = new BinaryWriter(fs))
                {
                    foreach (var arr in result1)
                    {
                        bwrite.Write(arr);
                    }
                }
            }
            //var bytes1 = File.ReadAllBytes(fs); ???
            sw1.Close();
zimmer9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You mean something like:
using System;
using System.IO;
using System.Text;

namespace EE_Q28702023
{
	class Program
	{
		static void Main(string[] args)
		{
			byte[] bytes = null;
			FileInfo file = new FileInfo("Products.xml");
			if (file.Exists)
			{
				using (FileStream fs = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
				{
					int bytesRead = Convert.ToInt32(fs.Length);
					bytes = new byte[bytesRead];
					fs.Read(bytes, 0, bytesRead);
				}
			}

			Console.WriteLine(Encoding.UTF8.GetString(bytes));
			Console.ReadLine();
		}
	}
}

Open in new window

Products.xml -
<?xml version="1.0" encoding="utf-8" ?>
<Products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<Product>
		<ID>1</ID>
		<Name>Toothpaste</Name>
		<Cost xsi:nil="true"/>
		<CreateDate>20140801</CreateDate>
		<CreateTime>132312</CreateTime>
	</Product>
	<Product>
		<ID>2</ID>
		<Name>Cough Syrup</Name>
		<Cost>5.49</Cost>
		<CreateDate>20130730</CreateDate>
		<CreateTime>152218</CreateTime>
	</Product>
	<Product>
		<ID>3</ID>
		<Name>Deodorant</Name>
		<Cost xsi:nil="true"/>
		<CreateDate>20120621</CreateDate>
		<CreateTime>172124</CreateTime>
	</Product>
	<Product>
		<ID>4</ID>
		<Name>Bar Soap</Name>
		<Cost>1.19</Cost>
		<CreateDate>20110511</CreateDate>
		<CreateTime>192030</CreateTime>
	</Product>
	<Product>
		<ID>5</ID>
		<Name>Liquid Soap</Name>
		<Cost xsi:nil="true"/>
		<CreateDate>20100401</CreateDate>
		<CreateTime>211936</CreateTime>
	</Product>
	<Product>
		<ID>6</ID>
		<Name>Colgate</Name>
		<Cost>2.99</Cost>
		<CreateDate>20140801</CreateDate>
		<CreateTime>132312</CreateTime>
	</Product>
	<Product>
		<ID>7</ID>
		<Name>Vicks</Name>
		<Cost xsi:nil="true"/>
		<CreateDate>20130730</CreateDate>
		<CreateTime>152218</CreateTime>
	</Product>
	<Product>
		<ID>8</ID>
		<Name>Right Guard</Name>
		<Cost>3.29</Cost>
		<CreateDate>20120621</CreateDate>
		<CreateTime>172124</CreateTime>
	</Product>
	<Product>
		<ID>9</ID>
		<Name>Dove</Name>
		<Cost xsi:nil="true"/>
		<CreateDate>20110511</CreateDate>
		<CreateTime>192030</CreateTime>
	</Product>
	<Product>
		<ID>10</ID>
		<Name>Dawn</Name>
		<Cost>3.49</Cost>
		<CreateDate>20100401</CreateDate>
		<CreateTime>211936</CreateTime>
	</Product>
</Products>

Open in new window

Which produces the following output -Capture.JPG-saige-
0
zimmer9Author Commented:
My goal is to read a file (assigned to bankFilePath) into a byte array so that I can inspect the values within the byte array at different locations within the array.

For example, I may look at var bytes1[1200] to see what byte is at this particular position
0
it_saigeDeveloperCommented:
Which I read the file into a byte array, I output the read bytes as a string that has been retrieved from the byte array.  More to the point the code that concerns you is here:
byte[] bytes = null;
string bankFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");
FileInfo file = new FileInfo(bankFilePath);
if (file.Exists)
{
	using (FileStream fs = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
	{
		int bytesRead = Convert.ToInt32(fs.Length);
		bytes = new byte[bytesRead];
		fs.Read(bytes, 0, bytesRead);
	}
}

Open in new window

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.