vb.net 如何在vb.net中保存文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2255986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save files in vb.net
提问by user225269
I'm trying to create an app launcher in vb.net but I do not know how to save files. Save files like the one that is executed when you run a setup for an application wherein the setup will save the application files on program files folder. I'm not trying to create a vb.net setup, because I want to run my program as portable. What I want the program to do is to place the files in their appropriate location when the user clicks a button Here's my current code:
我正在尝试在 vb.net 中创建一个应用程序启动器,但我不知道如何保存文件。保存文件,就像为应用程序运行安装程序时执行的文件一样,其中安装程序会将应用程序文件保存在程序文件文件夹中。我不是要创建 vb.net 设置,因为我想以可移植的方式运行我的程序。我希望程序做的是在用户单击按钮时将文件放在适当的位置这是我当前的代码:
Public Class Nircmd
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'shutdown
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Shutdown.lnk")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'monitor off
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Monitor Off.lnk")
End Sub
End Class
-Of course it won't work if the path doesn't contain the file specified. So I want to place another button that would do just that(to save the files specified in the desired folder. A simple syntax will do. Please
-当然,如果路径不包含指定的文件,它将不起作用。所以我想放置另一个按钮来做到这一点(以保存在所需文件夹中指定的文件。一个简单的语法就可以了。请
回答by Auguste
I don't quite understand, but I'll take a shot.
不是很明白,但我会拍一拍。
This will check to see if C:\foo\somefile.txt exists, and if it doesn't, create it and write some text:
这将检查 C:\foo\somefile.txt 是否存在,如果不存在,则创建它并写入一些文本:
If Not System.IO.File.Exists("C:\foo\somefile.txt") = True Then
Dim file As System.IO.FileStream
file = System.IO.File.Create("C:\foo\somefile.txt")
file.Close()
End If
My.Computer.FileSystem.WriteAllText("C:\foo\somefile.txt", "Some text")
If you want to copy or move a file, I think you'll want something like:
如果你想复制或移动一个文件,我想你会想要这样的:
System.IO.File.Copy("C:\foo\somefile.txt", "C:\bar\somefile.txt")
or
或者
System.IO.File.Move("C:\foo\somefile.txt", "C:\bar\somefile.txt")
回答by Tom M
I always use app.path
in order to make it portable. Not everyone's computer will assign the same drive letter.
我总是使用app.path
以使其便携。不是每个人的计算机都会分配相同的驱动器号。