在vb.net中使用打开文件和保存文件对话框 [英] Using the open file and save file dialogs in vb.net

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

问题描述

用户在打开文件对话框中选择了文件后,该如何处理?例如,如果用户选择了一个.txt文件并打开了它,那么如何从该文件中获取数据?如何返回用户在其中找到文件的路径?那么,如何保存文件?

Once a user has selected a file with the open file dialog, how can I handle this action? For example, if the user has selected a .txt file and has opened it, how can it get the data from the file? How can it return the path that the user found the file in? Then, how can it save the file?

我知道有一个OpenFileDialog.OpenFile()方法,但是我也很确定这不是我想要的.我也尝试了ToObject方法,但是我可能以某种方式弄乱了.

I know that there is a OpenFileDialog.OpenFile() method, but I am also pretty sure this is not what I am looking for. I have also tried the ToObject method, but I probably messed up somehow.

例如,是否有一种快速简便的方法来打开图像?

Is there a quick and easy way to open an image, for example?

感谢您的帮助!

这是在VB.net中.

This is in VB.net by the way.

推荐答案

Dim dlg_open As New OpenFileDialog()
If (dlg_open.Show() <> DialogResult.OK) Then Return

'if a textfile, then
Dim content As String() = IO.File.ReadAllLines(dlg_open.FileName)

'if an image, then
Dim img As New Bitmap(dlg_open.FileName)

您应该在处理IO的所有操作周围放置Try ... Catch块,否则将无法防止所有异常.

You should put Try...Catch blocks around all operations dealing with IO, you will not be able to prevent all exceptions.

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

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