Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:44:28+00:00 2026-05-27T14:44:28+00:00

I have a problem with reading a bunch of files using the following C#

  • 0

I have a problem with reading a bunch of files using the following C# code in my VS2008 project

public void FindFiles()
{
    //Root
    targetPath = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory()) + "WriteToCSVFolder";
}

public void ReadFiles()
{
    fileNameList_Original = Directory.GetFiles(targetPath);

    string defaultFileName = "file_";
    int counter = 0;

    foreach (string fileName in Directory.GetFiles(targetPath))
    {
        fullFileText_Original[counter] = File.ReadAllText(targetPath);
        //fileNameList_Original[counter]
        counter++;
    }

    counter = 0;
}

Now please consider im just fast ticking this so I haven’t bothered doing optimizations or anything yet. Just noticed that when I do a read action with the files NOT open and UAC (user account control) disabled on W7 64bit , and also not sharing it over network dropbox or anything else. It’s just some ABC BLA FOO files I just made and wanted to test, they are in the correct directory marked targetpath in my system folder and the program is being run from the correct drive.

Is it just something stupid in the code or?

And oh yeah , the application was marked as full trust.

Any ideas?

EDIT:

With the new idea implemented from the comment section below:

Changed the code from

public void ReadFiles()
{
    fileNameList_Original = Directory.GetFiles(targetPath);

    string defaultFileName = "file_";
    int counter = 0;

    foreach (string fileName in Directory.GetFiles(targetPath))
    {
        fullFileText_Original[counter] = File.ReadAllText(targetPath);
        //fileNameList_Original[counter]
        counter++;
    }

    counter = 0;
}

TO

public void ReadFiles()
{
    //Store all files names in a string array in one go
    fileNameList_Original = Directory.GetFiles(targetPath);

    string defaultFileName = "file_";
    int counter = 0;

    foreach (string fileName in Directory.GetFiles(targetPath))
    {
        //removed the storing file names, was redundant
        //added the suggested idea to the proper array
        fullFileText_Original[counter] = File.ReadAllText(fileName);
        //fileNameList_Original[counter]
        counter++;
    }

    counter = 0;
}

Gives me nullreference exception on File, not sure what my conclusion should be from this error. Admitting that I am pretty tired atm , probably going to realize exactly what it was on the way home 🙂

FINAL EDIT:

See answers my own post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:44:28+00:00Added an answer on May 27, 2026 at 2:44 pm

    This code works fine. I had to be fired for not fixing this almost…… trying to assign values to uninitialized arrays and ignoring any mutable size……. Never happened!

        foreach (string fileName in Directory.GetFiles(targetPath))
        {
            fileNameList_Original.Add(fileName);
    
            foreach (string text in File.ReadAllLines(fileName))
            {
                fullFileText_Original.Add(text);
                //fileNameList_Original[counter]
            }
        }
    

    Thanks for spotting the fileName instead of targetPath SGB! That was also a mistake I made!
    I fixed the rest of the problems on my own now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem reading OWL/XML files from Java using Jena. I have no
I have a problem with reading some gml files in c#. My files do
I have some problem with reading from sqlite3 database. -(void) readMessengesFromDatabase { sqlite3 *database;
Newbie question. In Python 2.7.2., I have a problem reading text files which accidentally
I have a problem with reading pdf file in Chrome by using PHP. The
I have a problem reading a txt file to insert in the mysql db
Android 2.1 update 1 Eclipse 3.5 I have a problem reading data from my
i have one problem controlling checkbox, i'm reading, and searching about the topic but
i have been reading about multiple inheritance What is the exact problem with multiple
My problem involves checking if I have a valid database connection before reading from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.