繁体   English   中英

如何在string []上将FileInfo转换为FileInfo []?

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

我解决了我的问题。 但是当我创建构造函数LI变量时的错误是ListViewItem但是我可以在foreach循环中使用它吗?

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

我正在尝试使用我的代码获取列表文件的长度:

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); 

这个显示错误是这样的:

无法从“字符串[]”转换为“字符串”

您不能转换class到它的完全相同该数组class 只需使用: FileInfo f = new FileInfo(filePath); 为你的foreach

另外,获取“文件列表的长度”将与filePaths.Length相同

如果需要长度,请改用filePaths.Length

如果要填充FileInfo ,请执行以下操作:

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;
}

并且您所有的文件信息都将在fileInfos ,如果您需要列表中的项目数量,请按如下所示通过CountfileInfos.Count

只需更改filePaths ,以filePathforeach循环

FileInfo f = new FileInfo(filePath);

一种替代方法是像这样更改循环:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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