gpt4 book ai didi

c# - 路径不是合法形式

转载 作者:行者123 更新时间:2023-11-30 13:47:32 26 4
gpt4 key购买 nike

当我尝试运行这个应用程序时,它说:“路径不是合法的形式。”。这是一个警告,它说有问题:“fileSystemWatcher1.IncludeSubdirectories = true;”当我点击浏览时。当我单击浏览第二个 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();
}
}

最佳答案

只是一个大胆的猜测,但也许您需要在更改 IncludeSubdirectoriesPath 之前将 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
}
}

http://msdn.microsoft.com/en-us/library/x7t1d0ky.aspx

关于c# - 路径不是合法形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com