I'm making a file downloader and I want it to download files to either my selected directoy or default directory.
Downloading code is here:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Button1.Text = "Updating..."
WebBrowser1.Visible = True
Dim uri As System.Uri = New System.Uri("http://199.91.154.170/e9f6poiwfocg/pei02c8727sa720/Ultra+v08.zip")
Dim webclient As System.Net.WebClient = New System.Net.WebClient()
Dim path As String =
If apppath = Nothing then
New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\\Test.zip"))
Else New String
(apppath)
End if
Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(path)
If Not System.IO.Directory.Exists(fileInfo.Directory.FullName) Then
System.IO.Directory.CreateDirectory(fileInfo.Directory.FullName)
End If
AddHandler webclient.DownloadFileCompleted, AddressOf webclient_DownloadDataCompleted
webclient.DownloadFileAsync(uri, path)
End Sub
Private Sub
And user selected path apppath is defined here:
If apppath = "" Then
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = Path.Combine(Environment.GetFolderPath( _
Environment.SpecialFolder.ApplicationData))
dialog.Description = "Select directory where to install the files"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = dialog.SelectedPath
End If
My.Computer.FileSystem.WriteAllText(apppath & " apppath.txt", apppath, False)
End If
How to fix Dim path As String else statement?
Thanks in advance!
If I am understanding your question correctly then I believe there is a simple solution, which is this:
Dim path As String
If apppath = Nothing then
path = New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\Test.zip"))
Else New String
path = apppath
End if
If I have not understood your requirements correctly, then please provide more information.
Related
I was trying to create function which will create a file in a specific folder. But every time when I am trying to create the file, it will shows me access denied. I have read a lot of solution, but i still have no clue on how to solving the problems.
Dim x as string
x = Path.GetDirectoryName(Application.ExecutablePath)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles APIsaveBtn.Click
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*|CSV Files (*.csv)|*.csv"
SaveFileDialog1.FileName = ""
Dim newArray As New ArrayList()
Dim path As String = x
If File.Exists(path) = True Then
System.IO.File.WriteAllText(path, "")
newArray.Sort()
My.Settings.API_Path = path
My.Settings.API_Key = APIkeyTxtBox.Text
Using sw As StreamWriter = New StreamWriter(path, True)
sw.WriteLine(APIkeyTxtBox.Text)
End Using
apikey = APIkeyTxtBox.Text
MessageBox.Show("Save Sucessfully", "Save")
ElseIf File.Exists(path) = False Then
Dim newFile As IO.StreamWriter = System.IO.File.CreateText(path)
newFile.Close()
My.Settings.API_Path = path
My.Settings.API_Key = APIkeyTxtBox.Text
Using sw As StreamWriter = New StreamWriter(path, True)
sw.WriteLine(APIkeyTxtBox.Text)
End Using
apikey = APIkeyTxtBox.Text
MessageBox.Show("Save Sucessfully", "Save")
End If
I have no idea on how to give permission to allow create file a specific folder.
I am trying to make an application which takes a file from your computer, renames that file with variables from 4 different combo boxes and then uploads it to an FTP server.
I have gotten everything working except the renaming part....
what i am trying to do is this.
slectedFile.pdf would become combobox1_combobox2_combobox3_combobox4.pdf
The file path is stored in a variable named FileName i know how to update FileName with the combobox values, but does it keep the original Path?
How would i go about doing this?
This is the code i have so far.
IP_box, User_Box and Pass_box are the textboxes for the appropriate server information.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(IP_Box.Text.ToString), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential(User_Box.Text.ToString, Pass_Box.Text.ToString)
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim file() As Byte
Try
System.IO.File.ReadAllBytes(FileName)
Catch ex As Exception
MessageBox.Show(ex.Message)
MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
End Try
Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
End Sub
Public Function OpenDialog()
Dim FD As OpenFileDialog = New OpenFileDialog()
FD.Title = "Selecteer een bestand"
FD.InitialDirectory = "C:\"
FD.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
FD.FilterIndex = 2
FD.RestoreDirectory = True
If FD.ShowDialog() = DialogResult.OK Then
Filename = System.IO.Path.GetFullPath(FD.FileName)
End If
End Function
Thank you in advance
At the end of your subroutine, you try to retrieve the file; you incorrectly use System.IO.Path.GetFullPath(FD.FileName) as FD.FileName already provides the full file name.
In order to rename the file to your desired name, you need to firstly evaluate the values of each ComboBox, of which can be done as a loop:
Private Function enumerateCheckboxes(ByVal path As String)
Dim fName As String
For Each Control In Me.Controls
If (TypeOf Control Is CheckBox AndAlso DirectCast(Control, CheckBox).Checked) Then
fName += CStr(Control.Name) + "_"
End If
Next
fName = path + fName.Substring(0, fName.Length - 1) + ".pdf"
Return fName
End Function
Public Function OpenDialog()
Dim FD As OpenFileDialog = New OpenFileDialog()
FD.Title = "Selecteer een bestand"
FD.InitialDirectory = "C:\"
FD.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
FD.FilterIndex = 2
FD.RestoreDirectory = True
If FD.ShowDialog() = DialogResult.OK Then
Dim Filename As String = FD.FileName
Filename = StrReverse(Filename)
Filename = Mid(Filename, InStr(Filename, "\"), Len(Filename))
Filename = StrReverse(Filename)
Return Filename
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenDialog()
End Sub
If I test with a file from the desktop:
However now we might possibly have an issue that the file does not exist, thus the program will crash. To fix this, we can quickly rename the file for the upload, and rename it back when it's finished.
The complete code:
Imports System.IO
Public Class Form1
Dim Filename As String
Dim originalFile As String
Public Function OpenDialog()
Dim FD As OpenFileDialog = New OpenFileDialog()
FD.Title = "Selecteer een bestand"
FD.InitialDirectory = "C:\"
FD.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
FD.FilterIndex = 2
FD.RestoreDirectory = True
If FD.ShowDialog() = DialogResult.OK Then
Filename = FD.FileName
Filename = StrReverse(Filename)
Filename = Mid(Filename, InStr(Filename, "\"), Len(Filename))
Filename = StrReverse(Filename)
Return Filename
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(IP_Box.Text.ToString), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential(User_Box.Text.ToString, Pass_Box.Text.ToString)
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim file() As Byte
Try
Filename = OpenDialog()
System.IO.File.ReadAllBytes(Filename)
Catch ex As Exception
MessageBox.Show(ex.Message)
MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
End Try
FileSystem.Rename(originalFile, Filename)
Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
FileSystem.Rename(Filename, originalFile)
End Sub
Private Function enumerateCheckboxes(ByVal path As String)
originalFile = path
Dim fName As String
For Each Control In Me.Controls
If (TypeOf Control Is CheckBox AndAlso DirectCast(Control, CheckBox).Checked) Then
fName += CStr(Control.Name) + "_"
End If
Next
fName = path + fName.Substring(0, fName.Length - 1) + ".pdf"
Return fName
End Function
End Class
I am having some troubles with FolderBrowserDialog
I've tried all the post I could find here and I'm almost there in terms of what I want.
following is my code:
Private Sub ButtonBrowseOutput_Click(sender As Object, e As EventArgs) Handles ButtonBrowseOutput.Click
Dim dialog = New FolderBrowserDialog()
dialog.SelectedPath = Application.StartupPath
If DialogResult.OK = dialog.ShowDialog() Then
TextBoxShowOutput.Text = dialog.ToString & "/helloforum" & ".txt"
End If
End Sub
would give me something like this:
System.Windows.Forms.FolderBrowserDialog/helloforum.txt
Where I want it to give it for example:
c:/users/sexyname/desktop/helloforum.txt
TextBoxShowOutput.Text = dialog.ToString & "/helloforum" & ".txt"
Must be:
TextBoxShowOutput.Text = dialog.SelectedPath & "/helloforum" & ".txt"
SelectedPath - Gets or sets the path selected by the user.
dialog.SelectedPath & "/helloforum.txt"
Just for your knowledge
Private Sub AbsolutePathOfDialogBoxes()
Dim dlgFolder = New FolderBrowserDialog
Dim dlgOpenFile = New OpenFileDialog
Dim dlgSaveFile = New SaveFileDialog
Dim absolutePath As String
'/*-----------------------------------*/'
absolutePath = dlgFolder.SelectedPath
absolutePath = dlgOpenFile.FileName
absolutePath = dlgSaveFile.FileName
'/*-----------------------------------*/'
End Sub
Happy Coding
Iam using visual studio 2012,
i would like to open "save dialog" to choose where to save my file instead of using fixed path,
the following code is a sample of what i would like to use it in:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
xlsWorkBook.SaveAs("C:\output\book1.xlsx")
xlsWorkBook.Close()
xls.Quit()
End Sub
i would like to change this path "C:\output\book1.xlsx" to save dialog, so i can choose where to save it manually.
thanks alot..
Like this, don't forget to dispose of com objects with the Marshal class like I added.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
Using sfd As New SaveFileDialog
If sfd.ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(sfd.FileName)
MessageBox.Show(sfd.Filename)
End If
End Using
xlsWorkBook.Close()
xls.Quit()
Marshal.FinalReleaseComObject(xlsWorkSheet)
Marshal.FinalReleaseComObject(xlsWorkBook)
Marshal.FinalReleaseComObject(xls)
End Sub
A little more comprehensive way to open the Save As Dialog than OneFineDay's answer (using the same method).
This opens the Save As dialog using a designated directory, filename, extension type, and window title; it also prompts before overwritting any existing files.
Dim dir as String = "C:\output\"
Dim fName As String = "Book1"
Using sfd As New SaveFileDialog
sfd.InitialDirectory = dir
sfd.Title = "Save As"
sfd.OverwritePrompt = True
sfd.FileName = fName
sfd.DefaultExt = ".xlsx"
sfd.Filter = "Excel Workbook(*.xlsx)|"
sfd.AddExtension = True
If sfd.ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(sfd.FileName)
End If
End Using
Add an openfiledialog to your form and then ...
With OpenFileDialog1
.Title = " whatever"
.InitialDirectory = "c:\"
.Multiselect = False
If .ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(.FileName)
End If
End With
You can use this :
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
xlsWorkBook.SaveAs("C:\output\book1.xlsx")
xls.Application.DisplayAlerts = False
xlsWorkBook.Close()
xls.Quit()
End Sub
In the first start of my application I need to specify a path to save some files to it. But in the open file dialogue it seems like that I have to select a file to open. How can I just specify a folder without oppening a file
like C:\config\
Here is my code
If apppath = "" Then
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.Title = "Select Application Configeration Files Path"
fd.InitialDirectory = "C:\"
fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True
If fd.ShowDialog() = DialogResult.OK Then
apppath = fd.FileName
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
End If
I need to select a file in order for it to work, but I just want to select a folder. So what's the solution?
You want to use the FolderBrowserDialog class instead of the OpenFileDialog class. You can find more information about it here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
For instance, you could do this:
If apppath = "" Then
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = "C:\"
dialog.Description = "Select Application Configeration Files Path"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = dialog.SelectedPath
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
End If
If I understand correctly, you want to let the user choose a folder. If that is the case, then you want to use FolderBrowserDialog instead of OpenFileDialog.
Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()
Or you can simply just make it less lines and very simple.
link: http://i.imgur.com/bMq0HNz.png
Start your dialog with a click:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
End Sub
Add if you want to show the actual path that you choose from your dialog
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString
End Sub
Use To:
Dim openFD As New OpenFileDialog()
Dim Directory As string = openFD.FileName
Try this
Private Sub BtnOpen_Click(sender As Object, e As EventArgs) Handles BtnOpen.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "x_pathfileforsend"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|*.zip|*.rar|*.ico|*.exe|*.png|*.bmp|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 5
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
txtpath.Text = openFileDialog1.FileName
End If
openFileDialog1.Dispose()
End Sub