shimshon

hi all.

i have a function that iterates a folder and do something with each file in this folder:

string[] ArrFiles=Directory.GetFiles(MyFolder)

for(int i=0;i<=ArrFiles.Length-1;i++)

{

Func(ArrFilesIdea;

}

NOW - what will happen if in the folder i have 5 files, and now i=3, and a user drops 2 more files to the folder.

will it affect the for..loop




Re: Visual C# General help with for ..next

Steve Py

No, if the system adds or removes files from the system the loop will not take this into account. It will loop through the array of file names as-of the .GetFiles() call. It's a good idea to check the existance of each file inside the loop, especially if the loop could take a while and the user is potentially be adjusting the contents of the directory. (Deleting, renaming, etc.)






Re: Visual C# General help with for ..next

Udhaya Kumar D

Else u add FileSystemWatcher to montior that particular directory for any changes. If it find any changes in the particular directory like file deleted, created, etc, then it will break the for loop.





Re: Visual C# General help with for ..next

Mattias Sjogren

Steve Py wrote:
It's a good idea to check the existance of each file inside the loop, especially if the loop could take a while and the user is potentially be adjusting the contents of the directory. (Deleting, renaming, etc.)

Even if you do such a check the file could have been deleted or renamed when your next operation executes.






Re: Visual C# General help with for ..next

Neel

We have similar sort of functionality in our project and if you want to monitor on real time activities on that particualr folder then you should go for FileWatcher(FW) component, in the event handler of the FW you can observe said folder and manipulate with files in that directory. There is a good article in msdn about implementing FW

http://blogs.msdn.com/ahamza/archive/2006/02/04/FileSystemWatcher-Duplicate-Events.aspx

Thanks