Solved

How would you write code using a loop to read in the individual files, 1 at a time, from a directory and process them?

Posted on 2016-08-30
3
52 Views
Last Modified: 2016-08-31
I am developing a C# application using VS2010 and .Net Framework 4.0

How would you write code using a loop to read in the individual files with a file name extension of "ard.out",
1 file at a time from a directory (for example: C:\BankABC\).

Then assign these *.ard.out file names to a variable X, so that the files could be read individually by "File.ReadAllBytes(X)" and then additional statements process these files.

byte[] imgBits = File.ReadAllBytes(X);
Do Some Additional Processing
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41776975
Here's a general approach, using GetFiles() to get a list of files and then loop through the list to load to a byte array:

//get a list of files in the directory
//GetFiles SearchOption can be set to look at subfolders.  Here I'm just looking at the current folder
//sourceDir would be "C:\BankABC\" in your case
string[] files = Directory.GetFiles(sourceDir, "*.out", SearchOption.TopDirectoryOnly);
foreach(string file in files) 
{
     string fileName = Path.GetFileName(file);
      byte[] imgBits = System.IO.File.ReadAllBytes(fileName);
     //Do some stuff
}

Open in new window

0
 

Author Comment

by:zimmer9
ID: 41777365
Why am I pointing to two different directory paths in the following code?
The filenames are the same.

My correct path is defined by TARGET_DIR_IN

However, "File.ReadAllBytes(fileName)" defines the path as \\m\USERS\z\My Documents\Visual Studio 2010\Projects\W\bin\Debug\20160824.ard.out'.

------------------
const string TARGET_DIR_IN = @"\\v\na\ctrls\dev\W\data\Test\";

string[] files = Directory.GetFiles(TARGET_DIR_IN, "*.out", SearchOption.TopDirectoryOnly);
            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                byte[] imgBits = File.ReadAllBytes(fileName);
0
 
LVL 42

Expert Comment

by:zephyr_hex (Megan)
ID: 41778234
I'm not sure I understand your question.  Are you saying that GetFiles() is not getting the files in TARGET_DIR_IN?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

785 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