如何使用打开文件对话框读取文件 [英] How to Read a file using open file Dialog Box

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

问题描述



我有一个浏览按钮和文本框,当我点击那个浏览按钮文件对话框打开时,现在关注的是该文件的选定路径应该在树视图中打开盒子..你能不能请帮帮我..

Hi
I have a browse button and text box and when i click on that browse button file dialog box is opening and now my concern is the selected path for that file should be opened in tree view box.. can you please help me in that..

推荐答案

从OpenFileDialog.FileName属性中获取文件名,并使用string.Split将其分解为它带有反斜杠字符的组件文件夹。



然后循环遍历数组中的每个字符串Split返回,为每个字符串创建一个新节点并将其添加到前一个Nodes属性中一个。

Get the file name from the OpenFileDialog.FileName property, and use string.Split to break it into it's component folders with the backslash character.

Then loop through each string in the array Split returns, creating a new node for each and add it into the Nodes property of the previous one.
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    string[] parts = ofd.FileName.Split('\\');
    TreeNodeCollection previousNodes = myTreeView.Nodes;
    foreach (string part in parts)
        {
        TreeNode node = new TreeNode(part);
        previousNodes.Add(node);
        previousNodes = node.Nodes;
        }
    myTreeView.ExpandAll();
    }


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

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