简体   繁体   中英

How to convert FileInfo to FileInfo[] on string[]?

I resolved my problem. But error when I was create constructor LI variable is ListViewItem but I can use this in foreach loop?.

ListViewItem LISTA = default(ListViewItem);
foreach (LISTA in this.lstImgAdded.SelectedItems) {

I'm trying to get Length of the list file with my code:

string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
List<FileInfo> fileInfos = new List<FileInfo>();  
foreach (string filePath in filePaths)
{
    FileInfo f = new FileInfo(filePaths);
    fileInfos.Add(f); 

This show error like this:

Cannot convert from 'string[]' to 'string'

You cannot convert a class to its exactly identical array of that class . Just use: FileInfo f = new FileInfo(filePath); for your foreach instead

Also, to get the "length of the list of file" will be identical to filePaths.Length

If you need the length, use filePaths.Length instead.

If you want to populate the FileInfo , do this instead:

string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop, false);
List<FileInfo> fileInfos = new List<FileInfo>();    
foreach (string filePath in filePaths)
{
    FileInfo f = new FileInfo(filePaths);
    fileInfos.Add(f);
    //long s1 = f.Length;
}

And all your file infos will be in the fileInfos and if you need the amount of item in the list, do it by Count like this: fileInfos.Count

Just change filePaths to filePath in the foreach loop

FileInfo f = new FileInfo(filePath);

An alternative would be change the loop like this:

foreach (int s1 in filePaths.Select(filePath => new FileInfo(filePath)).Select(f => ((FileInfo[]) f).Length))
{
  //Do somthing with the s1 here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM