10

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;
    }
}
}
Loko
  • 6,539
  • 14
  • 50
  • 78

5 Answers5

18

You're getting this error because you have a label named File on 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.Copy instead.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • I got a Label with the name:"File" and i changed the name. But it didn't work. – Loko Jun 10 '13 at 12:16
  • @loko Same error? Does your line still say `File.Copy`, or did it change to `myNewLabelName.Copy`? – Nolonar Jun 10 '13 at 12:18
  • 3
    And that's why you should stick to the coding conventions stating that variable names should always begin with a small letter. – Thorsten Dittmar Jun 10 '13 at 12:24
  • @ Michael Perrenoud can you also tell me how to add a cut option instead of a copy option? – Loko Jun 10 '13 at 12:29
  • @loko, gladly. Get started on it, post that code to a new question, and I'll lead you where I need to. – Mike Perrenoud Jun 10 '13 at 12:32
  • Ok so variable names should ALWAYS begin with a lowercase letter? – Loko Dec 08 '13 at 10:26
  • 1
    @Loko, well the issue here was that a Label had the same name as a class you were trying to reference. My conventions are usually type abbreviation + purpose for controls (e.g. lblFile) in this case. It does keep from conflicts but it's also easier to understand what I'm referencing. My class fields all begin with an _ and start with a lower case too. – Mike Perrenoud Dec 08 '13 at 12:26
8

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

System.IO.File.Copy(...
Andrei
  • 55,890
  • 9
  • 87
  • 108
6

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

I got into this mess after opening the solution in VS2012 and then opening it 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.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

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...

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

kasparspr
  • 146
  • 2
  • 8
0

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.