开发者

FileInfo Array From Strings

Currently I am getting the size of a file by using:

FileInfo fileSize = new FileInfo(@"C:\file.txt");
long size = fileSize.Length;

Rather than do this for every file I want, I thought you must be able t开发者_C百科o use an array and iterate through them so I used this:

FileInfo[] fileSizes = new FileInfo[2];
fileSizes[0] = @"C:\file.txt";
fileSizes[1] = @"C:\file.txt";

Now I keep getting the error "Cannot implicitly convert type 'string' to 'System.IO.FileInfo'". I tried using .ToString() but no dice, any help is very appreciated, thanks!


You shouldn't store multiple files in a FileInfo array. To fix your error, you need to do this:

FileInfo[] fileSizes = new FileInfo[3];
fileSizes[0] = new FileInfo(@"C:\file.txt");
fileSizes[1] = new FileInfo(@"C:\file.txt");
fileSizes[2] = new FileInfo(@"C:\file.txt");

However, you should store them like this (below) and iterate through the list and load one FileInfo at a time:

String[] files = new String[3];
files[0] = @"C:\file.txt";
files[1] = @"C:\file.txt";
files[2] = @"C:\file.txt";

foreach (String file in files)
{
    FileInfo info = new FileInfo(file);
    // do something with info.Length
}


FileInfo[] fileSizes = new FileInfo[3];
fileSizes[0] = new FileInfo(@"C:\file.txt");
fileSizes[1] = new FileInfo(@"C:\file.txt");
fileSizes[2] = new FileInfo(@"C:\file.txt");

@"C:\file.txt" is a string, not the object itself..


replace @"C:\file.txt"; with new FileInfo(@"C:\file.txt");

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜