C# &引用;a「;不包含“的定义”;b";没有扩展方法';b';接受类型为的第一个参数
我有一个无法修复的错误:C# &引用;a「;不包含“的定义”;b";没有扩展方法';b';接受类型为的第一个参数,c#,.net,winforms,C#,.net,Winforms,我有一个无法修复的错误: 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 as
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;
}
}
}
出现此错误是因为表单上有一个名为
File
的标签被引用,而不是System.IO.File
。您可以重命名标签
,这是我的建议,或者您可以使用System.IO.File.Copy
的完全限定路径。显然,您有一个名为File
的标签。它对System.IO.File
类进行阴影处理并导致错误。指定完整类名可以消除此问题:
System.IO.File.Copy(...
好的,我今天遇到了这个恼人的错误,“a”确实包含了“b”的定义
在VS2012中打开解决方案,然后在VS2010中打开解决方案后,我陷入了这种混乱
结果是删除受影响项目中的引用DLL,构建引用DLL项目,然后重新引用允许VS查看定义。请注意。在我重命名了类中的属性之后,在调试时我遇到了类似的错误。检查了所有内容,甚至在所有解决方案中搜索了ctrl+shift+f5组合的旧属性名称。找不到任何东西
过了一段时间,我注意到旧属性的输出值中存在带有“When hit…”条件的断点。还有一个简单的情况会发生这种情况。假设您在表单中添加了一个工具,如“按钮”。双击该工具后,(在Visual Studio中),将在Form1.cs中创建代码private void按钮1\u click(对象发送者,事件参数e){}
。代码this.button1.Click+=newsystem.EventHandler(this.button1\u Click)代码>也将在Form1.Designer.cs中创建。如果您删除了第一组代码(无论出于何种原因),那么您还需要删除第二位代码,否则将出现此错误。这可能是一种特定/简单的情况,但这是新程序员可能遇到的错误 我得到了一个名为“文件”的标签,我更改了名称。但它不起作用。@loko同样的错误?您的行是否仍然显示File.Copy
,还是更改为myNewLabelName.Copy
?这就是为什么您应该坚持编码约定,即变量名称应始终以小写字母开头。@Michael Perrenoud您能告诉我如何添加剪切选项而不是复制选项吗?@Loko,这里的问题是,一个标签与您试图引用的类具有相同的名称。在这种情况下,我的约定通常是控件(例如lblFile)的类型缩写+用途。它确实避免了冲突,但也更容易理解我引用的内容。我的类字段都以u开头,也以小写开头。我是在VS 2013中发现这个问题的,所以我按照你的建议做了,删除了引用并重新添加了它。修正了这个问题。