C# 我想在c应用程序中显示文件上载进度
我想在c windows应用程序中显示文件上载进度,因为我正在使用联机数据库。保存或上载文件需要更多的时间。我正在使用以下代码。我已经搜索了,但它没有显示准确的百分比进度。另外,我想在移动到其他位置之前更改文件名。 提前谢谢C# 我想在c应用程序中显示文件上载进度,c#,database,C#,Database,我想在c windows应用程序中显示文件上载进度,因为我正在使用联机数据库。保存或上载文件需要更多的时间。我正在使用以下代码。我已经搜索了,但它没有显示准确的百分比进度。另外,我想在移动到其他位置之前更改文件名。 提前谢谢 OpenFileDialog dlg = new OpenFileDialog(); DialogResult dlgRes = dlg.ShowDialog(); Application.DoEvents();
OpenFileDialog dlg = new OpenFileDialog();
DialogResult dlgRes = dlg.ShowDialog();
Application.DoEvents();
label14.Visible = true;
if (DialogResult.OK != DialogResult.Cancel)
{
foreach (string file in dlg.FileNames)
{
try
{
newpath = Path.Combine(textBox2.Text, Path.GetFileName(file)).ToString();
filenametemp = "tush" + Path.GetFileName(file).ToString();
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("please select folder to save");
}
else
{
File.Copy(file, Path.Combine(textBox2.Text, Path.GetFileName(file)).ToString());
if (dlgRes != DialogResult.Cancel)
{
//Provide file path in txtFilePath text box.
txtFilePath.Text = dlg.FileName;
}
//string folderpath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + textBox2.Text;
//string filePath = textBox2.Text + System.IO.Path.GetFileName(dlg.FileName);
//System.IO.File.Copy(dlg.FileName, folderpath, true);
try
{
//Read File Bytes into a byte array
byte[] FileData = ReadFile(txtFilePath.Text);
//Initialize SQL Server Connection
SqlConnection CN = new SqlConnection(ConfigurationManager.ConnectionStrings["Copymanagment"].ConnectionString);
//Set insert query
string qry = "insert into fileinfo (File_Id,File_Path,date,OriginalPath,Billnumber,Billdate,FileData) values(@FileId,@Filepath,@Date,@OriginalPath,@Billnumber,@Billdate,@FileData)";
//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, CN);
string path = textBox2.Text;
string[] arrpath = null;
int count;
arrpath = path.Split('\\');
for (count = 0; count <= arrpath.Length - 1; count++)
{
// MessageBox.Show(arrpath[count]);
}
string folder = arrpath[arrpath.Length - 1] + databaseid().ToString();
//We are passing Original File Path and File byte data as sql parameters.
string fileid = Path.GetDirectoryName(dlg.FileName);
SqlCom.Parameters.Add(new SqlParameter("@FileId", (object)folder));
SqlCom.Parameters.Add(new SqlParameter("@Filepath", (object)newpath));
SqlCom.Parameters.Add(new SqlParameter("@Date", (object)System.DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss tt")));
SqlCom.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtFilePath.Text));
SqlCom.Parameters.Add(new SqlParameter("@Billnumber", (object)textbillno.Text));
SqlCom.Parameters.Add(new SqlParameter("@Billdate", (object)dateTimePicker1.Value.ToString("dd-MM-yyyy")));
SqlCom.Parameters.Add(new SqlParameter("@FileData", (object)FileData));
//Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();
label14.Visible = false;
//Close form and return to list or Files.
MessageBox.Show("File saved Succsesfully");
txtFilePath.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
catch
{
MessageBox.Show("File already available");
}
}
}
按照您的方式,一条语句可以一次性插入整个数据,直到完成为止,而没有机会更新tge UI。您可以使用UPDATETEXT在chuncks中上载BLOB数据。有关如何执行此操作的完整说明,请参阅。这样,您将有机会更新prigress条的值。您可以使用asp.net ajax控件工具包。有一个ajax文件上传控件 注意:不要与没有进度条的同一库中的asynFile upload控件混淆 你可以在这里获得信息 要下载图书馆,请点击此处
对于windows应用程序,请尝试以下操作。您想显示png进度还是显示上载文件的百分比?上载文件的百分比您愿意使用第三方控件吗?如果任何第三方控件都可以完成我的工作,则可以。@Alex不需要第三方控件。先生,我提到过c windows应用程序。