伙计们,我尝试将所选项目从列表框复制到特定目录,但在(S)处获取错误 [英] Guys, I Try To Copy Selected Item From Listbox To Specific Directory, But Geting Error At (S)

查看:72
本文介绍了伙计们,我尝试将所选项目从列表框复制到特定目录,但在(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.

推荐答案

因为ListBox中的项是ListViewItems而不是字符串。试试这个:

Because the the items in the ListBox are ListViewItems and not strings. Try this:
foreach (ListViewItem item in listBox1.SelectedItems)
{
    sourceFolder = item.Text;


如果点击。对于ListBox,SelectedItems 并在Visual Studio IDE中按F12,然后您将看到明确显示
If you click on .SelectedItems for a ListBox and press F12 in Visual Studio IDE then you will be taken to the documentation which clearly shows
//
// 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


控件名为listBox1 ,但它似乎是ListView而不是ListBox。对于ListView ...



The control is named listBox1, but it appears to be a ListView and not a ListBox. For a ListView...

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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆