在没有保存文件对话框的情况下将生成的文件保存在硬编码的目录中 [英] Saving a generated file in a hardcoded directory without the savefile dialog

查看:61
本文介绍了在没有保存文件对话框的情况下将生成的文件保存在硬编码的目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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: 

推荐答案

那么您希望在哪里找到文件位置硬代码?

So where is the file location you desire to hard code?

你不知道该怎么做吗?

还是您仍要使用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?

IO.Path .合并

Environment.SpecialFolder枚举

如果不提供文件的位置,则只需要在全局字符串变量中对路径进行硬编码,似乎就没有必要为该路径提供代码了.

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

      

       


这篇关于在没有保存文件对话框的情况下将生成的文件保存在硬编码的目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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