Pregunta

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.

Hopefully somebody can help me out.

¿Fue útil?

Solución 2

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+X then navigate to Fundamentals > FileSystem

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top