【发布时间】:2015-08-18 12:17:10
【问题描述】:
我创建了一个程序来使用 filewatcher 监视记事本文件.. 每当文件中的文本发生更改时.. 我制作了一个 .exe 程序来运行... .exe 程序也可以正常运行.. 但它运行了两次...我搜索了 Stackoverflow 和其他网站.. 但我不明白我哪里出错了.. Frnds Plz 帮助我清除我的错误.. 我知道这个问题已经被问过很多次了.. 但是因为我是新手c#我听不懂..请帮我看看..
public Form1()
{
InitializeComponent();
}
private void filewatcher()
{
path = txtBoxPath.Text;
FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
fileSystemWatcher1.Path = path;
fileSystemWatcher1.Filter = "*.txt";
fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
fileSystemWatcher1.EnableRaisingEvents = true;
}
private void tbtextcopy()
{
Clipboard.SetText(File.ReadAllText(path));
this.textBox1.Text = Clipboard.GetText().ToString();
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
try
{
ProcessStartInfo PSI = new ProcessStartInfo(@"C:\Program Files\Ranjhasoft\Icon Changer\Icon changer.exe");
Process P = Process.Start(PSI);
P.WaitForExit();
P.Close();
}
finally
{
fileSystemWatcher1.EnableRaisingEvents = false;
}
}
private void BrowserBtn_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.SelectedPath = this.txtBoxPath.Text;
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
if (!string.IsNullOrEmpty(dlg.SelectedPath))
{
this.txtBoxPath.Text = dlg.SelectedPath;
}
}
if (string.IsNullOrEmpty(this.txtBoxPath.Text))
{
this.txtBoxPath.Text = appDataFolder;
}
if (!Directory.Exists(path))
{
MessageBox.Show("Specified folder does not exist");
}
}
private void txtBoxPath_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtBoxPath.Text))
{
this.filewatcher();
}
}
}
}
【问题讨论】:
-
所以@sstan 我应该使用这个fileSystemWatcher1.EnableRaisingEvents = true;在尝试命令而不是 filewatcher()
-
我也看过那篇文章。我想知道“我”哪里出错了...
-
我可以知道你得到了-2吗?
标签: c# forms file filesystemwatcher