Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the fileinfo of all files in a folder with GetFile()?

Tags:

I dont know whats my mistake.

FileInfo[] FileInformation = DirectoryInfo.GetFiles(textBoxPath.Text);   for (int i = 0; i <= FileInformation.Length; i++) {     File.Move(FileInformation[i].DirectoryName, FileInformation[i].Directory + "File" + i); } 

VisualSudio says that here is the error:

System.IO.DirectoryInfo.GetFiles(textBoxPath.Text);

like image 771
Julian Avatar asked Jul 12 '13 16:07

Julian


People also ask

What does directory GetFiles return?

GetFiles(String) Returns the names of files (including their paths) in the specified directory.


1 Answers

DirectoryInfo is not a static class (you mixed it with Directory which exposes static methods) thus you should create instance of it:

var dir = new DirectoryInfo(textBoxPath.Text); FileInfo[] files = dir.GetFiles(); 

Also I suggest you to use Path.Combine for generating new file path and FileInfo.MoveTo method, which don't require source directory name:

for(int i = 0; i < files.Length; i++) {     FileInfo file = files[i];     string destination = Path.Combine(file.DirectoryName, "File", i.ToString());     file.MoveTo(destination); }  

One more thought - if you don't need any additional info about files, besides names, then you can get file names only, without FileInfo objects creation. Use static methods of Directory and File classes. That will be more efficient:

string sourceDir = @"D:\Downloads";  string[] files = Directory.GetFiles(sourceDir); for (int i = 0; i < files.Length; i++) {     string fileName = files[i];     var destination = Path.Combine(sourceDir, "File", i.ToString());     File.Move(fileName, destination);              }  
like image 140
Sergey Berezovskiy Avatar answered Oct 19 '22 11:10

Sergey Berezovskiy