检查文件是否存在的代码(C#.net)
1
private void button1_Click(object sender, EventArgs e)
2
{
3
4
cn.ConnectionString = this.textBox1.Text;
5
cn.Open();
6
System.Data.DataTable dt = SqlHelper.ExecuteDataset(cn, CommandType.Text, " select * from [Resource_Files]").Tables[0];
7
this.progressBar1.Maximum = dt.Rows.Count;
8
for (int i = 0; i < dt.Rows.Count; i++)
9
{
10
int ID = Convert.ToInt32(dt.Rows[i]["FileId"]);
11
string path = dt.Rows[i]["FileName"].ToString() +"."+ dt.Rows[i]["Extend"].ToString() ;
12
string SQL = string.Empty;
13
path = @"F:\upfile\UploadFiles\" + path.Substring(0, 1) + @"\" + path;
14
if (File.Exists(path))
15
{
16
SQL = "update [Resource_Files] set [isExists]=1 where FileId= " + ID;
17
}
18
else
19
{
20
SQL = "update [Resource_Files] set [isExists]=0 where FileId= " + ID;
21
}
22
this.richTextBox1.Text = SQL+" path: "+path;
23
SqlHelper.ExecuteNonQuery(cn, CommandType.Text, SQL);
24
this.progressBar1.Value++;
25
this.progressBar1.Refresh();
26
this.richTextBox1.Refresh();
27
Application.DoEvents();
28
}
29
cn.Close();
30
MessageBox.Show("完成!");
31
32
33
34
}
private void button1_Click(object sender, EventArgs e)2
{3

4
cn.ConnectionString = this.textBox1.Text;5
cn.Open();6
System.Data.DataTable dt = SqlHelper.ExecuteDataset(cn, CommandType.Text, " select * from [Resource_Files]").Tables[0];7
this.progressBar1.Maximum = dt.Rows.Count;8
for (int i = 0; i < dt.Rows.Count; i++)9
{10
int ID = Convert.ToInt32(dt.Rows[i]["FileId"]);11
string path = dt.Rows[i]["FileName"].ToString() +"."+ dt.Rows[i]["Extend"].ToString() ;12
string SQL = string.Empty;13
path = @"F:\upfile\UploadFiles\" + path.Substring(0, 1) + @"\" + path;14
if (File.Exists(path))15
{16
SQL = "update [Resource_Files] set [isExists]=1 where FileId= " + ID;17
}18
else19
{20
SQL = "update [Resource_Files] set [isExists]=0 where FileId= " + ID;21
}22
this.richTextBox1.Text = SQL+" path: "+path;23
SqlHelper.ExecuteNonQuery(cn, CommandType.Text, SQL);24
this.progressBar1.Value++;25
this.progressBar1.Refresh();26
this.richTextBox1.Refresh();27
Application.DoEvents();28
}29
cn.Close();30
MessageBox.Show("完成!");31

32

33

34
}

浙公网安备 33010602011771号