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.