3

我在以下行收到一条错误消息:“路径不是合法形式”:

fileSystemWatcher2.EnableRaisingEvents = true;

这是我的代码:

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

这是 DestinationBox.Text

我需要它来向它传输文件,但我也想在其中使用 filewatch 会发生什么我用 FileSystemWatcher2 解决了它,但在 FileSystemWatcher1 仍然给我一个错误 在此处输入图像描述

4

1 回答 1

3

您没有使用dlgOpenDir(大概是 a FolderBrowserDialog)选择的路径。试试这个:

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

或者,如果你真的想显示正在被监视的文件夹,你可以这样做:

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
}
于 2013-06-11T11:33:30.087 回答