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

前端之家收集整理的这篇文章主要介绍了c# – “a”不包含“b”的定义,没有扩展方法’b’接受类型的第一个参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个错误,我不能修复:
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.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.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的完全限定路径.
原文链接:https://www.f2er.com/csharp/96952.html

猜你在找的C#相关文章