作者kevin888 (kevin)
看板C_Sharp
標題[問題] 判斷副檔名後將 資料匯入DATAGRID
時間Sun Oct 9 19:23:04 2011
以前玩了很多種將資料匯入DATAGRIDVIEW
但是有沒有方法可以讓我對檔案路徑字串的附檔名做判斷呢?
我的資料類型有TXT ACCESS SQL EXCEL
我取檔案完整路徑是用OpenFileDialog
然後我會將路徑字串顯示在TEXTBOX中
請問有辦法由判斷附檔名來 選擇資料讀取方式 嗎?
感謝各位!
剛無意中看到解法
private void button1_Click(object sender, EventArgs e)//來源位置
{
// 建立 OpenFileDialog 物件。
OpenFileDialog ofd = new OpenFileDialog();
// ofd.Filter = "txt(*.txt)|*.txt|" + "所有檔案|*.*";
// 設定 OpenFileDialog 物件的各個屬性。
ofd.CheckFileExists = true;
ofd.InitialDirectory = "D:\\";
ofd.RestoreDirectory = true;
ofd.Multiselect = false;
// 將使用者所選取之文字檔的完整路徑顯示於 textBox1 文字方塊中。
if (ofd.ShowDialog() == DialogResult.OK)
{ textBox1.Text = ofd.FileName;
string pao = Path.GetExtension(textBox1.Text);
if (pao == ".txt")
{ }
if (pao == ".mdb")
{ Show_Record(); }
if (pao == ".accdb")
{ Show_Record(); }
if (pao == ".mdf")
{ }
if (pao == ".sdf")
{ }
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.249.131.77
→ apiod:不就把副檔名取出來做判斷嗎? 10/09 20:35
※ 編輯: kevin888 來自: 111.249.131.77 (10/09 21:47)
→ kevin888:還有別的方法可以取副檔名嗎? 10/09 21:48
推 vulurn:Path.GetExtension 10/09 23:02