C# 路径不是合法的形式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17018984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The path is not of a legal form
提问by Loko
When I try to run this application it says:"The path is not of a legal form.". It's a warning and it says there is something wrong with:"fileSystemWatcher1.IncludeSubdirectories = true;" when I click on browse. When i click on browse for the 2nd filewatcher it does exactly the same. ( I have 2 browse buttons to watch 2 directories ) I gave the filewatchers no starting path but when i do give them a starting path, it works. I dont want that. Please help me.
当我尝试运行此应用程序时,它说:“路径不符合法律形式。”。这是一个警告,它说有问题:“fileSystemWatcher1.IncludeSubdirectories = true;” 当我点击浏览时。当我单击浏览第二个文件观察器时,它的作用完全相同。(我有 2 个浏览按钮来观看 2 个目录)我没有给文件观察者提供起始路径,但是当我给他们一个起始路径时,它就可以工作了。我不想要那个。请帮我。
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();
}
}
}
}
采纳答案by Torbj?rn Hansson
Just a wild guess, but perhaps you need to set EnableRaisingEventsto false before you change IncludeSubdirectories, and the Path? Like this:
只是一个疯狂的猜测,但也许您需要EnableRaisingEvents在更改之前设置为 false IncludeSubdirectories,而Path? 像这样:
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
}
}
回答by Rajeev Kumar
The path probably contains invalid characters. See the MSDN documentation on FileInfo constructor
路径可能包含无效字符。请参阅有关 FileInfo 构造函数的MSDN 文档
Put your code where you passing path in try block and catch the exception in catch block like this
将代码放在 try 块中传递路径的位置,并像这样在 catch 块中捕获异常
try
{
// Your code goes here
}
catch(Exception ex)
{
// If exception raise compiler comes here..
}
For more read about Try catch Blocks
有关Try catch 块的更多信息
private void button2_Click(object sender, EventArgs e)
{
try
{
fileSystemWatcher1.IncludeSubdirectories = true;
DialogResult resDialog = dlgOpenDir.ShowDialog();
if (resDialog.ToString() == "OK")
{
fileSystemWatcher1.Path = dlgOpenDir.SelectedPath;
textBox1.Text = dlgOpenDir.SelectedPath;
}
}
catch(Exception ex)
{
}
}

