日期:2014-05-17 浏览次数:21240 次
protected void button1_Click(object sender, System.EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;//设置打开文件类型 openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { if(openFileDialog1.FileName!= "") { MessageBox.Show("你选择了"+openFileDialog1.FileName);//得到文件路径 } } }
------解决方案--------------------
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开(Open)";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments
ofd.Filter = "文本文件(*.txt)|*.txt";
ofd.ValidateNames = true;
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(ofd.FileName, System.Text.Encoding.Default);
this.richTextBox1.Text = sr.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
------解决方案--------------------
OpenFileDialog dlg = new OpenFileDialog();
dlg .Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if(dlg.ShowDialog() == DialogResult.OK)
{
if(dlg.FileName!= "")
{
MessageBox.Show("文件名:"+dlg.FileName + "\n扩展名:" + Path.GetExtension(dlg.FileName);
}
}
------解决方案--------------------
string path = "controlKeyboard.exe"; //将文件存放在bin目录下 //string path = "D:\\controlKeyboard.exe"; //调用非bin目录下的程序路径 ProcessStartInfo startinfo = new ProcessStartInfo(path); startinfo.WindowStyle = ProcessWindowStyle.Normal; Process.Start(startinfo);