"a"中不包含对"b"的定义,也没有接受类型为"b"的第一个参数的扩展方法。

10

我遇到一个错误,无法解决:

Error 1 'System.Windows.Forms.Label' does not contain a definition for 'Copy' 
and no extension method 'Copy' accepting a first argument of     
type'System.Windows.Forms.Label' 
could be found (are you missing a using directive or an assembly reference?)
//path 156  22 FileWatcherEigen

那是我的错误。有人可以帮助我并解释一下出了什么问题吗?

这是我的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
    private bool pause = false;
    private bool cut1 = false;
    private bool copy1 = false;

    public Form1()

    {
        InitializeComponent();
    }



    // The lines with performed actions of a file
  private void fileSystemWatcher1_Created(object sender,System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }
    private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Changed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Created> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Deleted(object sender, System.IO.FileSystemEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Deleted> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    private void fileSystemWatcher2_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        if (!pause)
        {
            listBox1.Items.Add("File Renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        }
    }

    //1st directory
    private void button2_Click(object sender, EventArgs e)
    {
        if (dlgOpenDir.ShowDialog() == DialogResult.OK)
        {
            fileSystemWatcher1.EnableRaisingEvents = false;  // Stop watching
            fileSystemWatcher1.IncludeSubdirectories = true;
            fileSystemWatcher1.Path = dlgOpenDir.SelectedPath;
            textBox1.Text = dlgOpenDir.SelectedPath;         // Text of textBox2 = Path of fileSystemWatcher2
            fileSystemWatcher1.EnableRaisingEvents = true;   // Begin watching
        }
    }
    //2nd directory
    private void button3_Click(object sender, EventArgs e)
    {
        if (dlgOpenDir.ShowDialog() == DialogResult.OK)
        {
            fileSystemWatcher2.EnableRaisingEvents = false;  // Stop watching
            fileSystemWatcher2.IncludeSubdirectories = true;
            fileSystemWatcher2.Path = dlgOpenDir.SelectedPath;
            textBox2.Text = dlgOpenDir.SelectedPath;         // Text of textBox2 = Path of fileSystemWatcher2
            fileSystemWatcher2.EnableRaisingEvents = true;   // Begin watching
        }
    }
    //log
    private void button1_Click(object sender, EventArgs e)
    {

        DialogResult resDialog = dlgSaveFile.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            FileInfo fi = new FileInfo(dlgSaveFile.FileName);
            StreamWriter sw = fi.CreateText();
            foreach (string sItem in listBox1.Items)
            {
                sw.WriteLine(sItem);
            }
            sw.Close();
        }
    }
    //pause watching
    private void pause_button_Click(object sender, EventArgs e)
    {
        if (!pause)
        {
            pause = true;
            pause_button.Text = "Unpause";
        }
        else
        {
            pause = false;
            pause_button.Text = "Pause Watching";
        }
    }
    //clear listbox
    private void clear_button_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear(); 
    }

    private void Transfer_Click(object sender, EventArgs e)
    {
        if (copy1)
        {
            File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));
        }

        }

    private void Browse_file_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = openFileDialog1.ShowDialog();
        if (resDialog == DialogResult.OK)
        {
            FileBrowseBox.Text = openFileDialog1.FileName;
        }
    }

    private void Browse_destination_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = folderBrowserDialog1.ShowDialog();
        if (resDialog == DialogResult.OK)
        {
            DestinationBox.Text = folderBrowserDialog1.SelectedPath;
        }

    }

    private void CopyButton_CheckedChanged(object sender, EventArgs e)
    {
        copy1 = true;
    }

}
}

它说问题在这部分:

 File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileNameBox.Text, Path.GetExtension(FileBrowseBox.Text))));

我已经在这个论坛中尝试寻找答案或解决方案,但是我并没有真正找到答案。

使用以下代码可以运行:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    private bool cut = false;
    private bool copy = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
{
    File.Copy(FileBrowseBox.Text, Path.Combine(DestinationBox.Text, Path.ChangeExtension(FileBox.Text, Path.GetExtension(FileBrowseBox.Text))));
    label2.Text = "File Transfer Succeeded";
}

    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = openFileDialog1.ShowDialog();
        if (resDialog == DialogResult.OK)
        {
            FileBrowseBox.Text = openFileDialog1.FileName;
            label2.Text = "";
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        DialogResult resDialog = folderBrowserDialog1.ShowDialog();
        if (resDialog == DialogResult.OK)
        {
            DestinationBox.Text = folderBrowserDialog1.SelectedPath;
            label2.Text = "";
        }
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        copy = true;
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        cut = true;
    }
}
}
5个回答

18
你遇到了这个错误是因为你的表单上有一个名为 File 的标签被引用,而不是 System.IO.File。你可以重新命名 Label,我建议你这样做,或者你可以使用完全限定路径 System.IO.File.Copy

3
因此,您应该遵守编码约定,变量名始终应以小写字母开头。 - Thorsten Dittmar
@ Michael Perrenoud,您能告诉我如何添加一个剪切选项而不是复制选项吗? - Loko
@loko,好的。开始写代码,将其发布到一个新问题中,我会带领你到我需要的地方。 - Mike Perrenoud
变量名字应该始终以小写字母开头,对吗? - Loko
1
@Loko,这里的问题是一个标签与你试图引用的类具有相同的名称。我的惯例通常是控件类型缩写+用途(例如lblFile)。这确实可以避免冲突,但也更容易理解我正在引用什么。我的类字段都以_开头,并以小写字母开头。 - Mike Perrenoud
显示剩余2条评论

8

看起来您有一个名为FileLabel。它会覆盖System.IO.File类并导致错误。指定完整的类名应该可以解决这个问题:

System.IO.File.Copy(...

6

今天我遇到了一个烦人的错误,“a”确实包含“b”的定义。

我在VS2012中打开解决方案,然后又在VS2010中打开它,结果陷入了这个困境。

事实证明,通过删除受影响项目中的参考DLL,构建参考DLL项目,然后重新引用,可以让VS看到定义。


1
我在VS 2013遇到了这个问题,所以按照您的建议删除了引用并重新添加。问题得到解决。 - ian.aldrighetti

0

仅供参考。在调试时,我曾经因为重命名类中的属性而遇到过类似的错误。检查了所有内容,甚至使用ctrl+shift+f5在整个解决方案中搜索旧属性名称。但是没有找到任何问题...

过了一会儿,我注意到了一个现有断点,并设置了“命中时...”条件以输出旧属性的值。


0

还有一种简单的情况会发生。假设您向表单添加了一个"按钮"等工具。一旦您双击该工具(在Visual Studio中),代码private void button1_Click(object sender, EventArgs e) { }将在Form1.cs中创建。代码this.button1.Click += new System.EventHandler(this.button1_Click);也将在Form1.Designer.cs中创建。如果您删除了第一组代码(出于任何原因),那么您还需要删除第二部分代码,否则您将遇到此错误。这可能是一个特定/简单的情况,但这是新程序员可能遇到的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接