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
59 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

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 43

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 32
MYSQL responding very slow 3 48
Amazon S3 Images with .Net 3 29
Exception when using Alea null reference in fsharp.core.dll 3 52
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 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