如何在c#中使用打开文件对话框 [英] how to use open file dialog box in c#

查看:91
本文介绍了如何在c#中使用打开文件对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

请向我演示如何使用打开文件对话框。

我需要以用户身份选择文件,用户选择一个我需要获取文件路径的文件。我怎么能这样做。

Hello ,
Please demonstrate to me how i can use the open file dialog box.
I need to as the user to select a file , one the user selects the file i needs to get its file path. how can i do this.

推荐答案

你好..

在提出这个问题之前你一定用Google搜索了....



OpenFile DialogControl





Hi..
Before asking this question you must have googled it ....

OpenFile DialogControl


<pre lang="C++">
OpenFileDialog ofd = new OpenFileDialog();
 string filename = "";
 string path = "";        
  if (ofd.ShowDialog() == DialogResult.OK)      
    {                  
                filename = System.IO.Path.GetFileName(ofd.FileName);   
                  path = System.IO.Path.GetDirectoryName(ofd.FileName);     
     }     
     MessageBox.Show(filename, "Filename");      
    MessageBox.Show(path, "Directory");










这里是一些代码



Hi,

here is some code

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(openFileDialog1.FileName);
}





如果您只需要获取路径,只需搜索最后一个\在FileName中。



希望这有助于



If you need to get the path only, just search for the last "\" in the FileName.

Hope This helps


创建OpenFileDialog的对象

Create object of OpenFileDialog
OpenFileDialog opDialog = new OpenFileDialog();





处理FilePk事件





Handle the FilePk event

opDialog.FileOk += new CancelEventHandler(opDialog_FileOk);





显示对话框



show the dialog

opDialog.ShowDialog();



处理函数


handler function

void opDialog_FileOk(object sender, CancelEventArgs e)
    {
      string file_Name = opDialog.FileName;
    }


这篇关于如何在c#中使用打开文件对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆