Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 该路径不是合法形式_C#_Filesystemwatcher - Fatal编程技术网

C# 该路径不是合法形式

C# 该路径不是合法形式,c#,filesystemwatcher,C#,Filesystemwatcher,当我尝试运行这个应用程序时,它会说:“路径不是合法的形式。”。这是一个警告,当我单击browse时,它说“fileSystemWatcher1.IncludeSubdirectories=true”有问题。当我点击browse以查找第二个filewatcher时,它的操作完全相同。(我有2个浏览按钮来查看2个目录)我给了filewatchers没有起始路径,但当我给他们一个起始路径时,它就起作用了。我不想那样。请帮帮我 using System; using System.Collections

当我尝试运行这个应用程序时,它会说:“路径不是合法的形式。”。这是一个警告,当我单击browse时,它说“fileSystemWatcher1.IncludeSubdirectories=true”有问题。当我点击browse以查找第二个filewatcher时,它的操作完全相同。(我有2个浏览按钮来查看2个目录)我给了filewatchers没有起始路径,但当我给他们一个起始路径时,它就起作用了。我不想那样。请帮帮我

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
{
    private bool pause = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }


    // 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)
    {
        fileSystemWatcher1.IncludeSubdirectories = true;
        DialogResult resDialog = dlgOpenDir.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            fileSystemWatcher1.Path = dlgOpenDir.SelectedPath;
            textBox1.Text = dlgOpenDir.SelectedPath;
        }
    }
    //2nd directory
    private void button3_Click(object sender, EventArgs e)
    {
        fileSystemWatcher2.IncludeSubdirectories = true;
        DialogResult resDialog = dlgOpenDir.ShowDialog();
        if (resDialog.ToString() == "OK")
        {
            fileSystemWatcher2.Path = dlgOpenDir.SelectedPath;
            textBox2.Text = dlgOpenDir.SelectedPath;
        }
    }
    //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(); 
    }
}

}

路径可能包含无效字符。见

将代码放在try块中传递路径的位置,并在catch块中捕获异常,如下所示

try
{
    // Your code goes here
}
catch(Exception ex)
{
   // If exception raise compiler comes here.. 
}
有关更多信息,请阅读


这只是一个猜测,但在更改
IncludeSubdirectories
路径之前,可能需要将
EnableRaisingEvents
设置为false?像这样:

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;
        fileSystemWatcher1.EnableRaisingEvents = true;   // Begin watching
    }
}

它在哪一行崩溃?是的,我得到了“ArgumentException未处理”,所以他说没有路径。这是正常的,但我希望它不要给出那个错误。我的filewatcher立即开始监视,但在开始时不应该选择路径。因此,将其放在try-catch块中,它将简单地处理异常,如果第一次没有选择路径,应用程序将不会崩溃。你的确切意思是什么?你能发送一个代码示例吗?(我一周前刚开始学习C。)我应该在例外中加入什么?应该是这样吗try{fileSystemWatcher1.IncludeSubdirectories=true;}catch(异常示例){}您是否浏览了为try-catch提供的链接?这是c#中的基本概念。请把这个看一遍。。在try块中放入您认为可能引发异常的代码部分。
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;
        fileSystemWatcher1.EnableRaisingEvents = true;   // Begin watching
    }
}