-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.

1
  • Not even close. Saving file & getting information of the current directory are two different things mate. Commented Jun 18, 2013 at 9:24

2 Answers 2

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.

4
  • 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? Commented Jun 18, 2013 at 9: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
    Commented Jun 18, 2013 at 9:05
  • Here you have a couple of links of how to write a file: msdn.microsoft.com/en-us/library/6ka1wd3w.aspx stackoverflow.com/questions/2255986/how-to-save-files-in-vb-net
    – SysDragon
    Commented Jun 18, 2013 at 9:10
  • Got this problem solved, at the end it was quite easy. Should focus more. Thanks for the answers! Commented Jun 18, 2013 at 9: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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.