C# “a”不包含“b”的定义,并且没有接受第一个类型参数的扩展方法“b”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17023523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:22:51  来源:igfitidea点击:

"a" Does not contain a definition for"b" and no extension method ' b ' accepting a first argument of type

c#.netwinforms

提问by Loko

I got an error which i cant fix:

我遇到了一个无法修复的错误:

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

That was my error. Can someone help me and explain to me what went wrong?

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

This is my code:

这是我的代码:

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

}
}

It says the problem is within this part:

它说问题出在这部分:

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

I have tried to find it on this forum but i couldn't really find the answer or solution

我试图在这个论坛上找到它,但我真的找不到答案或解决方案

It does work with this code:

它确实适用于以下代码:

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

采纳答案by Mike Perrenoud

You're getting this error because you have a label named Fileon your form that's being referenced rather than System.IO.File. You can either rename the Label, which I'd recommend, or you can use the fully qualified path of System.IO.File.Copyinstead.

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

回答by Andrei

Apparently you have a Labelnamed File. It shadows the System.IO.Fileclass and causes the error. Specifying full class name should eliminate the problem:

显然你有一个Label名为File. 它隐藏System.IO.File类并导致错误。指定完整的类名应该可以解决这个问题:

System.IO.File.Copy(...

回答by Jeremy Thompson

Ok I got this annoying error today and “a” DIDcontain a definition for “b”.

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

I got into this mess after opening the solution in VS2012 and then opening it VS2010.

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

Turns out by deleting the reference DLL in the affected project, building the reference DLL project, then re-referencing allowed VS to see the definition.

结果是删除受影响项目中的引用 DLL,构建引用 DLL 项目,然后重新引用允许 VS 查看定义。

回答by hQuse

Just to note. I have got similar error while debugging, after I have renamed property in the class. Checked everything, even searched for old property name witch ctrl+shift+f5 in all solution. Nothing found...

只是要注意。在我重命名类中的属性后,我在调试时遇到了类似的错误。检查所有内容,甚至在所有解决方案中搜索旧属性名称女巫 ctrl+shift+f5。没有发现...

After a while I noticed existing breakpoint with 'When hit...' condition to output value of the old property.

过了一会儿,我注意到现有的断点与“当命中...”条件输出旧属性的值。

回答by Henry Mahoney

There is one other simple case where this will happen. Lets say you add a tool such as a "Button" to your form. Once you double click the tool,(in Visual Studio) the code private void button1_Click(object sender, EventArgs e) { }will be created in your Form1.cs. The code this.button1.Click += new System.EventHandler(this.button1_Click);will also be created in your Form1.Designer.cs. If you deleted the first set of code(for what ever reason) then you will also need to delete the second bit of code or else you will get this error. This may be a specific/simple situation but this is a error that newer programmers might run into.

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