如何将文件目录从表单发送到类 [英] how to send directory of a file from form to the class

查看:77
本文介绍了如何将文件目录从表单发送到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以一种形式说,我通过菜单栏选择要从桌面读取哪个文件.但是如何将文件的目录发送给该类,以便该类知道要读取哪个文件?该类已经拥有所有的readtext代码.谢谢.

Let say in a form,I select which file from the desktop is to be read through menu strip.but how to send directory of that file to the class so that the class know which file is to be read?The class already have all the readtext code.thanks.

推荐答案

Esmond,

好吧,首先,我想告诉您,如果您正在使用OpenFileDialog选择文件,它将返回所选文件的完整路径,因此您根本不需要指示驱动器来搜索文件.文件.只需传递完整的文件名即可.如果您使用任何其他方式来选择文件,请在您的问题中使用帖子中的改善问题"链接进行指定,以便我们更好地了解您的问题.



您可以像这样从文本文件中解析内容:

Hi Esmond,

Well, first I would like to let you know that if you''re using OpenFileDialog to select a file, it returns the full path of selected file or files, So you won''t need at all to indicate the drive to search a file. Just pass the complete file name which is absolute. If you''re using any other mean to select a file then please specify that in your question using "Improve question" link in your post so that we can get a better picture of your problem.



You can parse contents from a text file like this:

string DataInTextFile = "";

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Text Files|*.txt";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                StreamReader SR = new StreamReader(ofd.FileName);
                DataInTextFile = SR.ReadToEnd();
                SR.Close();
                SR.Dispose();
            }

            MessageBox.Show(DataInTextFile);



继续使用"DataInTextFile"字符串对象对文本文件内容进行操作.

希望这会有所帮助.

快乐编码:)



Go ahead with the manipulation on text file contents using "DataInTextFile" string object.

Hope this helps.

Happy Coding :)


这篇关于如何将文件目录从表单发送到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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