-1

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.

user2404495
  • 195
  • 1
  • 6
  • 17

2 Answers2

1

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.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • That didn't answer my question.. I need to SAVE the file without prompt. I know how to get the curr. directory. Can I save the file with SFD without a prompt? – user2404495 Jun 18 '13 at 09:02
  • `SaveFileDialog` simply lets the user select a file path, but didnt saves anything. You need to manually save your file with your data. – SysDragon Jun 18 '13 at 09:05
  • Here you have a couple of links of how to write a file: http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx http://stackoverflow.com/questions/2255986/how-to-save-files-in-vb-net – SysDragon Jun 18 '13 at 09:10
  • Got this problem solved, at the end it was quite easy. Should focus more. Thanks for the answers! – user2404495 Jun 18 '13 at 09:23
0

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

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143