如何将多个文件上传到一个文件夹,然后在 c# windows 应用程序的 datagridview 中显示这些文件
how to upload multiple files to a folder and then display that files in datagridview in c# windows application
我想上传文件夹中的文件,然后在数据网格视图中显示该文件
1) upload files to a folder
2) display that files in datagridview
谁能帮帮我
尝试将此代码放入您的表单中 class,然后根据需要对其进行扩展:
public string Path { get; set; }
private void UploadButton_Click(object sender, EventArgs e)
{
var o = new OpenFileDialog();
o.Multiselect = true;
if(o.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
o.FileNames.ToList().ForEach(file=>{
System.IO.File.Copy(file, System.IO.Path.Combine(this.Path, System.IO.Path.GetFileName(file)));
});
}
this.LoadFiles();
}
private void Form_Load(object sender, EventArgs e)
{
this.LoadFiles();
}
private void LoadFiles()
{
this.Path = @"d:\Test";
var files = System.IO.Directory.GetFiles(this.Path);
this.dataGridView1.DataSource = files.Select(file => new { Name = System.IO.Path.GetFileName(file), Path = file }).ToList();
}
这里是屏幕截图:
我想上传文件夹中的文件,然后在数据网格视图中显示该文件
1) upload files to a folder
2) display that files in datagridview
谁能帮帮我
尝试将此代码放入您的表单中 class,然后根据需要对其进行扩展:
public string Path { get; set; }
private void UploadButton_Click(object sender, EventArgs e)
{
var o = new OpenFileDialog();
o.Multiselect = true;
if(o.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
o.FileNames.ToList().ForEach(file=>{
System.IO.File.Copy(file, System.IO.Path.Combine(this.Path, System.IO.Path.GetFileName(file)));
});
}
this.LoadFiles();
}
private void Form_Load(object sender, EventArgs e)
{
this.LoadFiles();
}
private void LoadFiles()
{
this.Path = @"d:\Test";
var files = System.IO.Directory.GetFiles(this.Path);
this.dataGridView1.DataSource = files.Select(file => new { Name = System.IO.Path.GetFileName(file), Path = file }).ToList();
}
这里是屏幕截图: