本文介绍了伙计们,我尝试将所选项目从列表框复制到特定目录,但在(S)处获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button3_Click(object sender, EventArgs e)
        {
            string source,target; //fileToCopy, 
            string sourceFolder = @"\\192.168.5.10\fbar\TOOLS\ProbingApps\ProbingSystem\Clotho_License";
            string destFilename = (@"C:\Avago.ATF.Common\License");
            string newFileName = "Clotho";
            foreach (String s in listBox1.SelectedItems)
            //foreach (string s in listBox1.Items)
                try
            {
                    sourceFolder = s;  // <<< at here
                    source = Path.Combine(sourceFolder);
                    target = Path.Combine(destFilename);
                    File.Copy(source, target);
                    //File.Copy(s , destFilename);
                    {
                        File.Move(destFilename, newFileName);
                    }

                    {
                        Process.Start(@"C:\Avago.ATF.Common\License");
                    }
                }
            catch (Exception)
            {
                MessageBox.Show("Unable to browse or file cannot copied in the License Directory", "Unable");
            }                                                    













//我得到的错误(





// Error I get (

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsFormsApplication1.exe

Additional information: Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'System.String'.







如果我尝试使用Object,它会在s处显示红线。


)

If I try use Object, it getting red line at s.

推荐答案

foreach (ListViewItem item in listBox1.SelectedItems)
{
    sourceFolder = item.Text;


//
// Summary:
//     Gets a collection containing the currently selected items in the System.Windows.Forms.ListBox.
//
// Returns:
//     A System.Windows.Forms.ListBox.SelectedObjectCollection containing the currently
//     selected items in the control.



listboxitem不是字符串,它是一个对象(类型为ListBoxItem)。



在ListBox的情况下,将它转换为字符串就足够简单了


A listboxitem is NOT a string, it is an object (of type ListBoxItem).

In the case of a ListBox it is a simple enough matter to convert this to a string e.g.

sourceFolder = s.ToString();

但是你声称错误是

Quote:

附加信息:无法转换类型为'System.Windows.Forms。 ListViewItem '的对象以键入'System.String'。

Additional information: Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'System.String'.

因此,您不使用ListBox而是使用ListView。



如果你在ListViewItem上使用 .ToString()那么你会得到类似

Therefore you are not using a ListBox but a ListView.

If you use .ToString() on a ListViewItem then you will get something like

ListViewItem: {list of values}

在那个实例中你需要的是每个项目的第一个子项的Text,即

In that instance what you need is the Text of the first sub-item per item i.e.

sourceFolder = s.SubItems[0].Text;

sourceFolder = s.Text;

如果列表视图只有一列(或处于列表模式)

一般建议 - 为控件选择合适的名称并根据需要选择适当的控件

if the listview only has one column (or is in List mode)
Bit of general advice - choose appropriate names for your controls and choose the appropriate control for your needs


foreach (ListViewItem i in listBox1.SelectedItems)
{
     sourceFolder = i.Text;
     .
     .
     .
     etc...
}          


这篇关于伙计们,我尝试将所选项目从列表框复制到特定目录,但在(S)处获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:25