问题描述
您好,我最近编写了一个代码,该代码创建一个.bat文件,可以使用savefile.ShowDialog()保存该文件,但是我希望能够对将文件保存到的目录进行硬编码,以便随后稍后直接执行 代码.这是代码本身:
Hello, I recently wrote a code that creates a .bat file which can be saved using the savefile.ShowDialog(), however I would like to be able to hardcode the directory of where the file will be saved in order to then be able to directly execute it later on in the code. Here is the code itself:
推荐答案
你不知道该怎么做吗?
还是您仍要使用SaveFileDialog,但您希望将使用它提供的路径存储在全局变量中,以供以后在代码中使用?
Or are you still going to use SaveFileDialog but you want the path provided by using it stored in a global variable for later use in the code?
如果不提供文件的位置,则只需要在全局字符串变量中对路径进行硬编码,似乎就没有必要为该路径提供代码了.
Without you providing where the file is, if all you want is to hard code the path in a global string variable, it seems a moot point to provide code for that path.
您还创建了SaveFileDialog,但是从不丢弃创建的对象.使用/最终使用声明将执行此操作.同样,如果有人选择在SaveFileDialog上取消"savefile.ShowDialog"之后的代码,仍会运行导致您的应用 坠毁.以下是首选方法.
Also you create a SaveFileDialog but never dispose of the created object. A using/end using statement will do that. Also if somebody selected cancel on the SaveFileDialog your code after "savefile.ShowDialog" would still run causing your app to crash. The below would be a preferred method.
Using SFD As New SaveFileDialog
With SFD
.DefaultExt = ".bat"
.AddExtension = True
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
.Title = "Write bat file."
.Filter = "Bat files (*.Bat)|*.Bat"
End With
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SFD.FileName & ".bat", TextBox1.Text, False)
End If
End Using
这篇关于在没有保存文件对话框的情况下将生成的文件保存在硬编码的目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!