我目前正在完成我的家庭作业计划:)一切顺利,直到我坚持这个问题一段时间,我无法弄清楚它为什么不起作用。简而言之,我需要启用(如果文件存在)或禁用某些动态按钮。我使用带有计时器的FileSystemWatcher来检查女巫的某些文件夹.txt文件是由另一个Windows应用程序表单创建的。所以我想启用按钮,如果文件夹上出现某个文件,但我的代码不起作用。有人可以说为什么我的代码不起作用或问题可能隐藏在哪里?
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] stalnr = {"Stanliukas Nr.1","Staliukai Nr.2","Staliukai Nr.3","Staliukai Nr.4","Staliukai Nr.5","Staliukai Nr.6","Staliukai Nr.7","Staliukai Nr.8","Staliukai Nr.9","Staliukai Nr.10"};
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
Button[] staliukai = new Button[10];
string dir = @"C:\Users\Roberto\Documents\Visual Studio 2013\Projects\WindowsFormsApplication6\WindowsFormsApplication6\Uzsakymai\";
string flnm;
string fldir;
int StalID;
string[] staliukas = { "StaliukasNr1.txt", "StaliukasNr2.txt", "StaliukasNr3.txt", "StaliukasNr4.txt", "StaliukasNr5.txt", "StaliukasNr6.txt", "StaliukasNr7.txt", "StaliukasNr8.txt", "StaliukasNr9.txt", "StaliukasNr10.txt" };
private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button10.Enabled = false;
//Creating dynamical buttons
for (int i = 0; i < 10; i++)
{
flnm = staliukas[i];
fldir = dir + flnm;
staliukai[i] = new Button();
staliukai[i].Text = stalnr[i];
staliukai[i].Height = 40;
staliukai[i].Width = 100;
staliukai[i].Location = new Point(20, 50 * i + 55);
staliukai[i].Parent = panel1;
// First check if there is some files in folder
if (!File.Exists(fldir))
{
staliukai[i].Enabled = false;
}
else
{
staliukai[i].Enabled = true;
}
staliukai[i].Click += staliukai_Click;
staliukai[i].Tag = i;
}
panel1.AutoScroll = true;
}
/*Watcher should check for changes in certain folder for file changes and new file creation and use the function to enable button*/
private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
timer1.Enabled = true;
patikrinimas();
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
timer1.Enabled = true;
patikrinimas();
}
/*Function for checking if file exists*/
void patikrinimas()
{
for (int i = 0; i < 10; i++)
{
Button temp = staliukai[i];
flnm = staliukas[i];
fldir = dir + flnm;
if (!File.Exists(fldir))
{
temp.Enabled = false;
}
else
{
temp.Enabled = true;
}
}
}
答案 0 :(得分:1)
您可以修改这些代码,然后就可以了。
1.在Form1_Load中添加fileSystemWatcher1.Path。
this.fileSystemWatcher1.Path = this.dir;
2.设置 timer1.Start()并将 patikrinimas()从fileSystemWatcher1_Changed和fileSystemWatcher1_Created移至 timer1_Tick 。
private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
timer1.Enabled = true;
timer1.Start();
//patikrinimas();
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
timer1.Enabled = true;
timer1.Start();
//patikrinimas();
}
private void timer1_Tick(object sender, EventArgs e)
{
patikrinimas();
}
再试一次。