开发者

Troubles with arrays c#

开发者 https://www.devze.com 2023-01-18 21:21 出处:网络
I\'ve been tasked with writing a c# program which is meant to do the following: You input the target directory.

I've been tasked with writing a c# program which is meant to do the following:

You input the target directory. The program searches if the directory exists. If it doesn't, request new input. Otherwise carry on.

Program gets files of a certain type (it's meant to be .epl files, but for working this out I'm just using .txt files) and adds them to the FilePathsA[] array. An Array List is made from FilePathsA.

If the count of the list is 0; get file. (there will only ever be 1 file of its type in said folder). If the count of the list is 1; take the filepath string, create a text reader instance. Read the text of the file, append the text, send the text to a printer.

Close text reader instance. Delete the file that was just printed. Clear the array list.

Rep开发者_如何学Pythoneat the get file process until a new file is found, print/delete that, repeat the cycle. It's meant to stay active until the program is shut down.

Getting the initial file works fine; it prints then deletes the file from the folder. However my issue is with the recursion in perpetuating this cycle. Once a file is deleted, it'll continuously "search" but not actually follow through. Debugging seems to indicate that it's not getting the new file at all.

Not sure how to go about solving this, here is the code excerpt: http://pastebin.com/RNn7QqXJ

Any help appreciated!


I think you should put

string[] filePathsA;
ArrayList filePaths;

outside your loop

and

filePathsA = Directory.GetFiles(@dir, "*.txt");
filePaths = new ArrayList(filePathsA);

inside your do ... while block in its beginning.


Why don't you:

while( !shutdown )
{
   foreach( string filename in Directory.GetFiles(@dir, "*.txt")
   {
      printTheFile( filename );
      File.Remove( filename );
      Thread.Sleep(3000); //if you need to sleep
   }
   Thread.Sleep(3000); //if you need to sleep
}

Or even better set up a System.IO.FileSystemWatcher and handle file creations.

0

精彩评论

暂无评论...
验证码 换一张
取 消