无法将类型“string”隐式转换为“string []”

时间:2022-05-01 16:05:30

I'm trying add items to listbox with name and path, I'm getting this eror:

我正在尝试使用名称和路径向列表框添加项目,我得到这个错误:

Cannot implicitly convert type string to string[]

无法将类型字符串隐式转换为字符串[]

string path = textbox1.Text;
string[] FileName, FilePath;

DirectoryInfo directoryInfo = new DirectoryInfo(path);
FileInfo[] files = directoryInfo.GetFiles();

foreach( FileInfo fileInfo in files)
{
    FileName = fileInfo.FullName; // Here 
    FilePath = fileInfo.Name; // and here 
    listbox2.Items.Add(fileInfo.FullName);
}

5 个解决方案

#1


4  

You've declare this:

你已经声明了这个:

string[] FileName, FilePath;

So assigning this:

所以分配这个:

FileName = fileInfo.FullName; // Here 
FilePath = fileInfo.Name; // and here

Is assigning strings (fileInfo.FullName is a string for example) to arrays of strings.

将字符串(fileInfo.FullName是一个字符串)分配给字符串数组。

What you're trying to do is make a list of these, so define lists and add the values:

你要做的是列出这些,所以定义列表并添加值:

string path = textbox1.Text; 
var FileNames = new List<string>(); 
var FilePaths = new List<string>();

DirectoryInfo directoryInfo = new DirectoryInfo(path); 
FileInfo[] files = directoryInfo.GetFiles();

foreach( FileInfo fileInfo in files) {
    FileNames.Add(fileInfo.FullName); // Here 
    FilePaths.Add(fileInfo.Name); // and here 
    listbox2.Items.Add(fileInfo.FullName); 
}

#2


0  

You've defined FileName and FilePath as string[]. It appears like you intended them to each contain single string values.

您已将FileName和FilePath定义为string []。看起来你打算每个都包含单个字符串值。

string[] FileName, FilePath;

change them to string

将它们更改为字符串

string FileName, FilePath;

#3


0  

string[] FileName, FilePath;

You declare FileName and FilePath as arrays, but FullName and Name both return string, hence the error.

您将FileName和FilePath声明为数组,但FullName和Name都返回字符串,因此出错。

FileSystemInfo.FullName Property
FileSystemInfo.Name Property

FileSystemInfo.FullName属性FileSystemInfo.Name属性

Change your declaration to:

将您的声明更改为:

    string FileName, FilePath;

#4


0  

The problem is that FileName and FilePath are declared as string[], but you're attempting to assign a string to them. However, since you say you're trying to add a list of files with name and path to a listbox, you don't need the FileName or FilePath variables at all. What would be easier is to set the DataSource of the ListBox and set which property of the object to show:

问题是FileName和FilePath声明为string [],但您正在尝试为它们分配字符串。但是,由于您说您正在尝试将名称和路径的文件列表添加到列表框中,因此根本不需要FileName或FilePath变量。更容易的是设置ListBox的DataSource并设置要显示的对象的哪个属性:

listbox2.DisplayMember = "Name"; //The Name property from FileInfo
listbox2.DataSource = files;

Then, when it comes time to get the selected item, cast it as FileInfo:

然后,当需要获取所选项目时,将其强制转换为FileInfo:

var selectedFileInfo = (FileInfo)listbox2.SelectedItem;
var selectedFileName = selectedFileInfo.Name;
//etc

#5


0  

It may prove better to simply declare FileNames and FilePaths to type List<string> and do Add in a single foreach loop, but here is a linq alternative.

简单地声明FileNames和FilePaths类型为List 并在单个foreach循环中执行Add可能更好,但这里有一个linq替代方案。


string[] FileNames, FilePaths;

using anonymous function delegates, in two linq operations.

在两个linq操作中使用匿名函数委托。

FileNames = files.Select( f => f.FullName).ToArray();
FilePaths = files.Select( f => f.Name).ToArray();
listBox2.AddRange(files.Select(FileNames)); //AddRange can take either IEnumerable<T> or T[] as a parameter

If you build the expression before hand, it becomes a little bit more clear (and perhaps more optimized), for you're not declaring anonymous delegate functions.

如果你事先构建表达式,它会变得更清晰(也许更优化),因为你没有声明匿名委托函数。

// these can be declared out of function scope as readonly..
Func<FileInfo, string> SelectFullName = (f) => f.FullName;
Func<FileInfo, string> SelectName = (f) => f.Name;

FileNames = files.Select(SelectFullName).ToArray();
FilePaths = files.Select(SelectName).ToArray();
listBox2.AddRange(FileNames);

The advantage to the linq method is that you're not going to be Enumerating through FileNames and FilePaths when adding items

linq方法的优点是,在添加项目时,您不会通过FileNames和FilePath枚举

#1


4  

You've declare this:

你已经声明了这个:

string[] FileName, FilePath;

So assigning this:

所以分配这个:

FileName = fileInfo.FullName; // Here 
FilePath = fileInfo.Name; // and here

Is assigning strings (fileInfo.FullName is a string for example) to arrays of strings.

将字符串(fileInfo.FullName是一个字符串)分配给字符串数组。

What you're trying to do is make a list of these, so define lists and add the values:

你要做的是列出这些,所以定义列表并添加值:

string path = textbox1.Text; 
var FileNames = new List<string>(); 
var FilePaths = new List<string>();

DirectoryInfo directoryInfo = new DirectoryInfo(path); 
FileInfo[] files = directoryInfo.GetFiles();

foreach( FileInfo fileInfo in files) {
    FileNames.Add(fileInfo.FullName); // Here 
    FilePaths.Add(fileInfo.Name); // and here 
    listbox2.Items.Add(fileInfo.FullName); 
}

#2


0  

You've defined FileName and FilePath as string[]. It appears like you intended them to each contain single string values.

您已将FileName和FilePath定义为string []。看起来你打算每个都包含单个字符串值。

string[] FileName, FilePath;

change them to string

将它们更改为字符串

string FileName, FilePath;

#3


0  

string[] FileName, FilePath;

You declare FileName and FilePath as arrays, but FullName and Name both return string, hence the error.

您将FileName和FilePath声明为数组,但FullName和Name都返回字符串,因此出错。

FileSystemInfo.FullName Property
FileSystemInfo.Name Property

FileSystemInfo.FullName属性FileSystemInfo.Name属性

Change your declaration to:

将您的声明更改为:

    string FileName, FilePath;

#4


0  

The problem is that FileName and FilePath are declared as string[], but you're attempting to assign a string to them. However, since you say you're trying to add a list of files with name and path to a listbox, you don't need the FileName or FilePath variables at all. What would be easier is to set the DataSource of the ListBox and set which property of the object to show:

问题是FileName和FilePath声明为string [],但您正在尝试为它们分配字符串。但是,由于您说您正在尝试将名称和路径的文件列表添加到列表框中,因此根本不需要FileName或FilePath变量。更容易的是设置ListBox的DataSource并设置要显示的对象的哪个属性:

listbox2.DisplayMember = "Name"; //The Name property from FileInfo
listbox2.DataSource = files;

Then, when it comes time to get the selected item, cast it as FileInfo:

然后,当需要获取所选项目时,将其强制转换为FileInfo:

var selectedFileInfo = (FileInfo)listbox2.SelectedItem;
var selectedFileName = selectedFileInfo.Name;
//etc

#5


0  

It may prove better to simply declare FileNames and FilePaths to type List<string> and do Add in a single foreach loop, but here is a linq alternative.

简单地声明FileNames和FilePaths类型为List 并在单个foreach循环中执行Add可能更好,但这里有一个linq替代方案。


string[] FileNames, FilePaths;

using anonymous function delegates, in two linq operations.

在两个linq操作中使用匿名函数委托。

FileNames = files.Select( f => f.FullName).ToArray();
FilePaths = files.Select( f => f.Name).ToArray();
listBox2.AddRange(files.Select(FileNames)); //AddRange can take either IEnumerable<T> or T[] as a parameter

If you build the expression before hand, it becomes a little bit more clear (and perhaps more optimized), for you're not declaring anonymous delegate functions.

如果你事先构建表达式,它会变得更清晰(也许更优化),因为你没有声明匿名委托函数。

// these can be declared out of function scope as readonly..
Func<FileInfo, string> SelectFullName = (f) => f.FullName;
Func<FileInfo, string> SelectName = (f) => f.Name;

FileNames = files.Select(SelectFullName).ToArray();
FilePaths = files.Select(SelectName).ToArray();
listBox2.AddRange(FileNames);

The advantage to the linq method is that you're not going to be Enumerating through FileNames and FilePaths when adding items

linq方法的优点是,在添加项目时,您不会通过FileNames和FilePath枚举