How to move files from folder to another using the paths listed in a ListBox - vb.net

I have a list box that has paths to files. How can I move the selected file items that have the paths to another folder?

You can do it like this :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each SelectedItem In ListBox1.SelectedItems
Try
File.Move(SelectedItem.ToString(), "New path here")
Catch : End Try
Next
End Sub
And with error handling, so if a file is not accessible it will continue to the next one.
Hope that helped you :)

Related

Simple application with opening webpage in VB

I would like to create simple exe app in VB, which will open default browser with specified page. I have this code
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("www.google.com")
End Sub End Class
If I click on the button I get error message:
System.ComponentModel.Win32Exception: System cannot find this file
Could someone help me where is the problem? Or is there any other approach how to open webpage?
"System cannot find this file" means that your system is not reading the URL as one and it's trying to get the resource at the local path: "www.google.com" which obviously doesn't exist. Check this link because the issue seems to be the same.
Here in an application I call Best Links is the process I use to view URL's.
It works by storing website URL links in a SQLite Database (DB).
The DB also stores the Site Name and the Last Visit Date.To view the site the user just clicks on the DB entry displayed in DataGridView. Screen Shot Below. And the code that opens the link in what ever your default browser is.
Private Sub dgvLinks_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLinks.CellClick
selRow = e.RowIndex
If e.RowIndex = -1 Then
gvalertType = "4"
frmAlert.ShowDialog()
Exit Sub
End If
Dim row As DataGridViewRow = Me.dgvLinks.Rows(e.RowIndex)
If row.Cells(2).Value Is Nothing Then
gvalertType = "5"
frmAlert.ShowDialog()
Return
Exit Sub
ElseIf gvTxType = "View" Then
webPAGE = row.Cells(2).Value.ToString()
siteID = CInt(row.Cells(0).Value.ToString())
UpdateSiteData()
Process.Start(webPAGE)
Data Grid View
If you would like the complete code or and exe file let me know and I will add it to my GitHub repository code will be a ZIP file.

Why Is A FilePath Item With ~$ Added To ListBox

I am adding file paths from a folder to a List Box which are then opened as text in a Rich Text Box. I have used the same syntax as the code below for achieving the same purpose in another List Box and it works just fine. But, in the current example, I have two files in the default MyProjects folder (i.e. default folder is created by my app), but when I add the file paths from the folder as items to the List Box, I get a third item with ~$ in the file path? This item is obviously some kind of repetition of the first file path in the list? The two files in the default folder are also created by my app so, if this is a file access issue, I don't understand why I wouldn't have access to a file created by my app? Can anyone give me a clue what's happening here?
What I have Tried:
I have tried debugging to check where the extra file path is coming from. As far as I can tell, it is being created when I add the file paths to the List Box? i.e. commenting out the code for adding the items to the List Box stops all items being added, but doesn't tell me where this extra item is coming from?
The "Extra Item" Issue:
System.Windows.Forms.ListBox+ObjectCollectionC:\Users\username\Documents\MySolution\MyProjects\RTFdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects\Testdoc.rtf
C:\Users\username\Documents\MySolution\MyProjects~$Fdoc.rtf
The Code:
lbxName.Items.AddRange(Directory.GetFiles("C:\Users\" + username + "\Documents\MySolution\MyProjects"))
lbxName.SelectedIndex = 0
Code For Loading:
For Each item In lbxName.SelectedItems
RTB.LoadFile(lbxName.SelectedItem, RichTextBoxStreamType.RichText)
Next
I cannot reproduce the error in the following code.
Private Sub FillListBox()
ListBox1.Items.AddRange(Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects"))
ListBox1.SelectedIndex = 0
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillListBox()
End Sub
Please read the comments in the following code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item In ListBox1.SelectedItems
'The following will overwrite the contents of the RichTextBox on each iteration
'This overload of LoadFile will only handle .rtf files
RichTextBox1.LoadFile(item.ToString)
Next
End Sub
I suggest you set the SelectionMode property to One in the designer and do the following.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.LoadFile(ListBox1.SelectedItem.ToString)
End Sub
Try to use as follows:
Directory.GetFiles("C:\Users\" & username & "\Documents\MySolution\MyProjects").Where(Function(f)
Return New IO.FileInfo(f).Attributes & IO.FileAttributes.Hidden & IO.FileAttributes.System = 0

Visual Basic, make application only open in a specific folder

Is there a way I can make a Visual Basic .net app open only if it's located in a specific folder/path or if it contains a certain element inside the folder?
Thanks.
you could use this to check for folder or file in application path:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'check if folder exists
If Not IO.Directory.Exists(Application.StartupPath & "\yourdirname") Then
Application.Exit()
End If
'check if file exists
If Not IO.File.Exists(Application.StartupPath & "\yourdirname\yourfile.ext") Then
Application.Exit()
End If
End Sub
End Class
Easy, On load event:
If not Application.StartupPath() = "C:\" then
Application.Exit()
End if

How to get the filepath of an image dropped onto a button in VB

I am attempting to get the filepath of an image that is dropped onto a button in VB. I have set the button to allow drop and I have set up this segment to hold the event code:
Private Sub btnDropZone_drop(sender As Object, e As EventArgs) Handles btnDropZone.DragDrop
End Sub
How would I go about getting the filepath from this event as a string? Thanks in advance!
A button is a pretty small drop target, but it would work the same as anything else: examine e As DragEventArgs:
Private Sub btnDropZone_drop(sender As Object,
e As EventArgs) Handles btnDropZone.DragDrop
'ToDo check the format to see what was dropped
Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each s As String In Files
ListBox1.Items.Add(Path.GetFileName(s))
Next
End Sub
When you are trying to figure out what to do in an event, always look up the meaning and contents of the eventargs

Save file path on show dialog

I want to save path on text box after Directory 1: whenever I try to close the application and open again the path is not there.I want it to show every time I open the application.
Private Sub btnRootBrowse1_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse1.Click
RootFolderBrowserDialog.ShowDialog()
txtPath1.Text = RootFolderBrowserDialog.SelectedPath
End Sub
You need save your data (in this case txtPath1.Text) to Hard-Drive (File or DataBase) and reload it in the next execution.
It can be easy when you use Application-Setting:
In the Solution Explorer, Double-Click "My Project"
in left pane click "Setting"
in the table, Fill in the fields as needed, example for you case: Name: DirectoryLocation Type: String Scope: User Value: Empty.
Sample Code Using:
Public Sub New()
InitializeComponent()
'load from the hard-disk
txtPath1.Text = My.Settings.DirectoryLocation
End Sub
Private Sub btnRootBrowse1_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse1.Click
RootFolderBrowserDialog.ShowDialog()
txtPath1.Text = RootFolderBrowserDialog.SelectedPath
'save to hard-disk
My.Settings.DirectoryLocation = txtPath1.Text
My.Settings.Save()
End Sub