c# 选择本地文件(多选、单选)

选择本地文件,多选或者单选的情况。读取文件位置和文件名。

       //选择文件
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = @"C:\desktop";
            openFileDialog.Filter = "文本文件(*.txt)|*.txt";
            openFileDialog.Multiselect = false; //是否可以多选true=ok/false=no
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //单个文件
                //string localFileName = openFileDialog.FileName;
                //ShowMsg($"当前读取文件:{localFileName}", 2);
                //多个文件
                string[] strNames = openFileDialog.FileNames;
                for (int i = 0; i < strNames.Length; i++)
                {
                    listBox1.Items.Add();
                    listNames.Add(strNames[i]);
                }
            }
        }

 

 

你可能感兴趣的:(c#)