Click here to Skip to main content
16,010,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a requirement in my code where i need the below statement in For loop:
C#
foreach (FileInfo fi in source_s.GetFiles())
{
   fi.CopyTo(Path.Combine(dest_d.ToString(), fi.Name), true);
}

help me if anybody knows..
Posted
Updated 26-May-10 19:55pm
v2
Comments
Sandeep Mewara 27-May-10 1:57am    
Your question does not makes much of a sense. Please elaborate on what you are trying to do and what is expected?
Arun Jacob 27-May-10 2:09am    
Why you are trying to convert this?whats your exact requirement?

To avoid repeated calls to GetFiles(), which could slow your application down considerably, get the files once before starting the for loop.

FileInfo[] foundFiles = source_s.GetFiles();
for (int i = 0; i < foundFiles.Length; i++)
{
  foundFiles[i].CopyTo(Path.Combine(dest_d.ToString(), foundFiles[i].Name), true);
}


or even
C#
FileInfo[] foundFiles = source_s.GetFiles();
for (int i = 0; i < foundFiles.Length; i++)
{
  FileInfo fi = foundFiles[i];
  fi.CopyTo(Path.Combine(dest_d.ToString(), fi.Name), true);
}

which avoids another repeated data fetch.
 
Share this answer
 
Firstly I dont see why you should be converting foreach to for.

If you still want to do it get the count of all files into a variable
and then run a for loop -
for(int i=0;i++,i<count)
{
      fi.CopyTo(Path.Combine(dest_d.ToString(), source_s.GetFiles()[i].Name), true);
}


where count is the list of your files.

I'm not absolutely sure if source_s.GetFiles()[i].Name this work as I dont know what source_s.GetFiles() is returning....
 
Share this answer
 
v2
Comments
sainath437 27-May-10 2:15am    
in foreach loop i have given reference to "fi" as fileinfo(FileInfo fi in source_s.GetFiles()) but in for loop how can i give reference to fi
Abhinav S 27-May-10 3:10am    
Try source_s.GetFiles()[i].FileInfo.Name. You should be able to access the FileInfo inside GetFiles().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900