Visual Basic表单编码(如何设置目录)(Visual Basic Form Coding (how to set a directory))

编程入门 行业动态 更新时间:2024-10-28 16:26:04
Visual Basic表单编码(如何设置目录)(Visual Basic Form Coding (how to set a directory))

Private Sub Button7_Click(sender As Object,e As EventArgs)处理Button7.Click

Dim Newline As String Newline = System.Environment.NewLine System.IO.File.WriteAllLines("C:\Users\Sang\Desktop\filename.txt", Result1.Lines) System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", Result2.Lines) System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", values.Lines) End Sub

这是我在桌面上制作文本文件的编码。 但是,我的朋友无法运行此代码,因为此代码仅供我自己使用,如上所示。 我想使用folderbroswerdialog来为每个人推广这个编码。 具体来说,如果用户在表单上按下此按钮,文件夹浏览器应该询问他要保存此文本文件的位置,文本文件应保存在定向文件夹或桌面中。 我试着通过查看许多YouTube视频和资源来自己做,但我失败了。 我该怎么办呢?

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

Dim Newline As String Newline = System.Environment.NewLine System.IO.File.WriteAllLines("C:\Users\Sang\Desktop\filename.txt", Result1.Lines) System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", Result2.Lines) System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", values.Lines) End Sub

This is my coding for making a text file on my desktop. However, my friend can not run this code because this code is only for myself as you can see above. I would like to use a folderbroswerdialog to generalize this coding for everyone. To be specific, if a user pressed this button on the form, folder browser should ask him where he wants to save this text file and text file should be saved in the directed folder or desktop. I tried to do it on my own by looking at many youtube videos and resources but I failed. How should I proceed this?

最满意答案

您可以使用Environment.SpecialFolder枚举,其中包含可以使用Environment.GetFolderPath方法检索的系统目录的大小:

Dim DesktopDir As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

然后,您可以使用Path.Combine方法正确组合目录/文件路径:

Dim OutputFile As String = IO.Path.Combine(DesktopDir, "filename.txt")

然后:

IO.File.WriteAllLines(OutputFile, "Text Here")

You can use the Environment.SpecialFolder Enumeration which contains the ubications of the system's directories that you can retrieve using Environment.GetFolderPath method :

Dim DesktopDir As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

Then you can use the Path.Combine method to properlly combine a directory/file path:

Dim OutputFile As String = IO.Path.Combine(DesktopDir, "filename.txt")

Then:

IO.File.WriteAllLines(OutputFile, "Text Here")

更多推荐

本文发布于:2023-07-07 09:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   如何设置   目录   Basic   Visual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!