路径不是合法的表格C#

时间:2013-06-11 11:22:15

标签: c# winforms

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

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

我需要它来传输一个文件,但我也想在文件中看到会发生什么 我用FileSystemWatcher2解决了它,但仍然在FileSystemWatcher1给我一个错误 enter image description here

1 个答案:

答案 0 :(得分:3)

您没有使用dlgOpenDir选择的路径(大概是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
}