VB.NET 保存文件没有保存对话框提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17164433/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:56:51  来源:igfitidea点击:

VB.NET Saving file without save dialog prompt

vb.netvisual-studiosavepromptcodedom

提问by user2404495

I need to know how I can save a file, without using save file dialog prompt.

我需要知道如何在不使用保存文件对话框提示的情况下保存文件。

Currently my code is:

目前我的代码是:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ProgressBar1.Value = 0
        Using sfd As New SaveFileDialog
            With sfd
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    .DefaultExt = "exe"
                    .Filter = "Saved.exe (*.exe)|*.exe"
                    '---Save File---
                    '---Code to pack the result with UPX packer---
        ProgressBar1.Value = 100
        MsgBox("Success.", MsgBoxStyle.Information)
    End Sub

And I would like to know, how I can save the file with the name "Saved.exe" to the same folder where my application is, without the save file dialog's prompt. The file needs to be saved on the same folder where the program is, with preconfigured name so the UPX packer knows what to pack.

我想知道,如何在没有保存文件对话框提示的情况下,将名为“Saved.exe”的文件保存到我的应用程序所在的同一文件夹中。该文件需要保存在程序所在的同一文件夹中,并使用预配置的名称,以便 UPX 打包程序知道要打包的内容。

Hopefully somebody can help me out.

希望有人可以帮助我。

回答by SysDragon

There is a lot of different ways to get the directory:

有很多不同的方法来获取目录:

My.Application.Info.DirectoryPath
Application.Current.BaseDirectory
IO.Path.GetDirectoryName(Application.ExecutablePath)
Windows.Forms.Application.StartupPath
IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().CodeBase)

Once you have the application folder, simply add the name for the filename you want to get a full filename path.

获得应用程序文件夹后,只需添加要获得完整文件名路径的文件名即可。

回答by Matt Wilko

The save file dialog just allows the user to specify where they want to save the file.

保存文件对话框只允许用户指定他们想要保存文件的位置。

If you want to save a file without this dialog you will have to specify the filename yourself.

如果要在没有此对话框的情况下保存文件,则必须自己指定文件名。

You can use one of the following code snippets to save the file:

您可以使用以下代码片段之一来保存文件:

For a Text File:

对于文本文件:

My.Computer.FileSystem.WriteAllText("C:\SomeDir\YourFile.txt", "Text", True)

For a Binary file:

对于二进制文件:

Dim fileContents() As Byte = {244, 123, 56, 34}
My.Computer.FileSystem.WriteAllBytes("C:\SomeDir\YourFile.bin", fileContents, True)

Note: These are straight from the standard snippets in Visual Studio. Press Ctrl+K, Ctrl+Xthen navigate to Fundamentals > FileSystem

注意:这些直接来自 Visual Studio 中的标准片段。按Ctrl+KCtrl+X然后导航至Fundamentals > FileSystem