Loko
Loko

Reputation: 6679

Path is not a legal Form C#

I get an error saying:"Path is not a legal form" at the line:

fileSystemWatcher2.EnableRaisingEvents = true;

Here is my code:

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


            fileSystemWatcher1.EnableRaisingEvents = false;  // Stop watching
            fileSystemWatcher1.IncludeSubdirectories = true;
            fileSystemWatcher1.Path = Path.GetDirectoryName(FileBrowseBox.Text);         // Text of textBox2 = Path of fileSystemWatcher2
            fileSystemWatcher1.EnableRaisingEvents = true;   // Begin watching

    }

    private void Browse_destination_Click(object sender, EventArgs e)
    {

        if (dlgOpenDir.ShowDialog() == DialogResult.OK)
        {
            fileSystemWatcher2.EnableRaisingEvents = false;  // Stop watching
            fileSystemWatcher2.IncludeSubdirectories = true;
            DestinationBox.Text = dlgOpenDir.SelectedPath;
            fileSystemWatcher2.Path = DestinationBox.Text;
            fileSystemWatcher2.EnableRaisingEvents = true;   // Begin watching
        }
    }

This is the DestinationBox.Text

I need it to transfer a file to it but i also wanna filewatch in it what happens I Solved it with FileSystemWatcher2 but still gives me an error at FileSystemWatcher1 enter image description here

Upvotes: 3

Views: 2555

Answers (1)

Yuck
Yuck

Reputation: 50825

You're not using the path selected by dlgOpenDir (presumably a FolderBrowserDialog). Try this instead:

if (dlgOpenDir.ShowDialog() == DialogResult.OK)
{
    fileSystemWatcher2.EnableRaisingEvents = false;  // Stop watching
    fileSystemWatcher2.IncludeSubdirectories = true;
    fileSystemWatcher2.Path = dlgOpenDir.SelectedPath;
    fileSystemWatcher2.EnableRaisingEvents = true;   // Begin watching
}

Or, if you really want to show the folder that's being watched you could do this:

if (dlgOpenDir.ShowDialog() == DialogResult.OK)
{
    fileSystemWatcher2.EnableRaisingEvents = false;  // Stop watching
    fileSystemWatcher2.IncludeSubdirectories = true;
    DestinationBox.Text = dlgOpenDir.SelectedPath;
    fileSystemWatcher2.Path = DestinationBox.Text;
    fileSystemWatcher2.EnableRaisingEvents = true;   // Begin watching
}

Upvotes: 3

Related Questions