jiaopou4385
2018-01-09 13:32
采纳率: 66.7%
浏览 1.2k

c# datagridview 读取文件

1.点击按钮,读取视频文件 在datagridview中显示文件的名称和路径
2.点击按钮,读取一个文件夹,文件夹中有n个视频文件,在datagridview中显示左右文件的名称和路径。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • codybin 2018-01-10 09:27
    已采纳
        int dataGridView1_Rows = 0;
        int dataGridView1_Cells = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter ="(所有文件)|*.*";
            if (openFileDialog.ShowDialog()==DialogResult.OK)
            {
                dataGridView1.Rows.Add();
                string path = openFileDialog.FileName;
                dataGridView1.Rows[dataGridView1_Rows].Cells[dataGridView1_Cells].Value = Path.GetFileName(path);
                dataGridView1_Cells=1;
                dataGridView1.Rows[dataGridView1_Rows].Cells[dataGridView1_Cells].Value = Path.GetFullPath(path);
                dataGridView1_Rows++;
                dataGridView1_Cells = 0;
            }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string path = folderBrowserDialog.SelectedPath;
                string[] s = Directory.GetFiles(path);
                foreach (string ss in s)
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[dataGridView1_Rows].Cells[dataGridView1_Cells].Value = Path.GetFileName(ss);
                    dataGridView1_Cells = 1;
                    dataGridView1.Rows[dataGridView1_Rows].Cells[dataGridView1_Cells].Value = Path.GetFullPath(ss);
                    dataGridView1_Rows++;
                    dataGridView1_Cells = 0;
                }
            }
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
        }
    
    点赞 评论
  • blownewbee 2018-01-09 13:40
     this.DataGridView1.DataSource = Directory.GetFiles(x:\\你的路径, "*.avi", SearchOption.AllDirectories).Select(x => new { 名称 = Path.GetFileName(x), 路径 = x });
    
    
    点赞 评论
  • sanshaoaa 2018-01-10 01:32

    要代码还是要思路??

    点赞 评论
  • woziji777888 2018-01-10 02:47

    1.读取文件路径
    2.遍历查找文件的相关文件名称
    3.显示在datagridview

    点赞 评论

相关推荐 更多相似问题