如何使用C#Windows窗體完成文件瀏覽功能:
1、簡單界面如下:
2、點擊上傳按鈕瀏覽本地文件:
3、顯示上傳文件名:
步驟:
1、在主界面上加入openFileDialog窗體插件。
2、進入代碼部分的對應的點擊事件:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "C:\\";//初始加載路徑爲C盤;
openFileDialog1.Filter = "文本文件 (*.txt)|*.txt";//過濾你想設置的文本文件類型(這是txt型)
// openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";(這是全部類型文件)
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = Path.GetFileName(openFileDialog1.FileName);//顯示文件的名字
}
}