vb.net ~ How to add folder names from a directory to a list box - vb.net

I'm trying to add folder names from within a directpory specified by the user to a list box. I have tried a few solutions, but can't seem to add any items. Most recently I tried:
For Each folder As String In System.IO.Path.GetDirectoryName("D:\")
ListBox1.Items.Add(folder)
Next
The form was built using VB in VB Studio Express 2013. There were no errors when running the program.
If anyone can point me in the right direction, then please help!

If you want to have a list of Directories you need to call Directory.GetDirectories(path), not Path.GetDirectoryName(path) that, in your case returns just null (passing the root directory of a drive)
For Each folder As String In System.IO.Directory.GetDirectories("D:\")
ListBox1.Items.Add(folder)
Next
if you want to show only the folder name and not the full path, just use
For Each folder As String In System.IO.Directory.GetDirectories("D:\")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
Yes, I know, it seems wrong to ask for GetFileName over a folder name, but passing a full path to GetFileName returns just the folder without the path.

how to select folder name to preview picture
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each folder As String In System.IO.Directory.GetDirectories("C:\Program Files (x86)\KONAMI\Pro Evolution Soccer 2016\SFXPath\SweetFX")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem = "folder" Then PictureBox1.ImageLocation = "C:\Program Files (x86)\KONAMI\Pro Evolution Soccer 2016\SFXPath\SweetFX\HD Natural by pimplo\preview.jpg"
End Sub
End Class

Related

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

Deleting folders VB

I am making a program that will list mods in a game. Ive got the code for it down here. But the mod directory-location is not the proper way show it.
Imports System
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' make a reference to the mod folder
Dim di As New IO.DirectoryInfo("I:\Steam\steamapps\common\Kerbal Space Program\GameData")
' find the folders
Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
Dim dra As IO.DirectoryInfo
' find the files
Dim diar As IO.FileInfo() = di.GetFiles()
Dim drag As IO.FileInfo
' make a list of all folders in the main folder
For Each dra In diar1
Liste.Items.Add(dra)
Next
' make a list of all files in the main folder
For Each drag In diar
Liste.Items.Add(drag)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Liste.SelectedIndexChanged
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
End Class
So instead of the whole directory-location being told to the listbox "I:\Steam\steamapps\common\Kerbal Space Program\GameData" It would automaticly find the \KSP\GameData directory. Could that be done? If so, could anyone explain, either with a snippet with code or just a simple explaining, how i could do this?
Bonus questions:
Could you use VB to copy a mod folder from a WinRar file over to the mod directory? If so, could anyone explain, either with a snippet with code or just a simple explaining, how i could do this?
Could i "disable" the mod folders, so the game wouldnt detect them, but could still be "enabled" again? (Instead of deleting the mod completely) If so, could anyone explain, either with a snippet with code or just a simple explaining, how i could do this?
I HAVE tried google, though i am searching for someone who could explain it to me.
Copying files out of a rar (or other compressed file) is possible, I've done it in Java, although I do not know how to do it in VB.
As for the second question, for disabling, just rename the folder (or even the individual mods themselves by giving them a different extension, e.g. someMod.ext.disabled). That should be trivial.

The system cannot find the reference specified for an assembly

I have a VB.NET solution which have a few projects and a 'References' folder in the root directory.
Now I want to add a new dll reference to one of the projects from this References folder but getting the following message:
The system cannot find the reference specified
How can this be solved?
I encountered this many times. Here I was trying to execute a jar file when a picturebox was clicked. I added my jar to my resources :
Private Sub PictureBox9_Click(sender As Object, e As System.EventArgs) Handles _PictureBox9.Click
Dim dir As String = My.Computer.FileSystem.SpecialDirectories.Temp
Dim filename As String = dir + "minecraft.jar" 'Look Below
IO.File.WriteAllBytes(filename, My.Resources.minecraft)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "minecraft.jar")
End Sub
The following worked for other programs though, but not minecraft, :
Private Sub Panel9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel9.Click
proc = Process.Start("vbexpress.exe", "")
End Sub
*proc was a New System.Diagnostics.Process()
But for actual files, you have manually lure them out of their location.
So I did Me.Resources.1234.png which saved me writing the directory. But this still did not work with minecraft, so I kept that long paragraph about in place.

VB.NET - Choose folder by browsing and copy file from application directory

Hello StackOverflow users. I am trying to create an application where you can browse to a folder, press install button and it will copy some files to the directory of your choosing?
I found some example code but
how do i go on with my code from here? Cant figure out how to copy the files. You can see at last in the code i tried to copy files but its not really working, how do i use the function? I want the files to come from the application directory. And copy to the browsed folder.
Public Class Installer
Private Sub Installer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
End Sub
Private Sub BrowseButton_Click_1(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click
' Declare a variable named theFolderBrowser of type FolderBrowserDialog.
Dim theFolderBrowser As New FolderBrowserDialog
' Set theFolderBrowser object's Description property to
' give the user instructions.
theFolderBrowser.Description = "Please browse to your GTAIV directory."
' Set theFolderBrowser object's ShowNewFolder property to false when
' the a FolderBrowserDialog is to be used only for selecting an existing folder.
theFolderBrowser.ShowNewFolderButton = False
' Optionally set the RootFolder and SelectedPath properties to
' control which folder will be selected when browsing begings
' and to make it the selected folder.
' For this example start browsing in the Desktop folder.
theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
' Default theFolderBrowserDialog object's SelectedPath property to the path to the Desktop folder.
theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' If the user clicks theFolderBrowser's OK button..
If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's
' SelectedPath property.
Me.FolderTextBox.Text = theFolderBrowser.SelectedPath
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles InstallButton.Click
My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, theFolderBrowser.SelectedPath)
End Sub
End Class
SnoX
When you call this line of code
My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, _
theFolderBrowser.SelectedPath)
the theFolderBrowser object cannot be referenced because it was a local variable inside another method. However, before exiting, you have copied the selected path into a textbox. You could use that textbox as destination of your copy
My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath, _
Me.FolderTextBox.Text )
And also keep in mind that CopyFile copies only one file, if you intend to copy an entire folder you need the CopyDirectory method. There is another important detail in this method:
If your files exist in the destination directory and you don't use the overload with the overwrite flag you will get an exception
My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath, _
Me.FolderTextBox.Text, True )

How to save files in vb.net

I'm trying to create an app launcher in vb.net but I do not know how to save files. Save files like the one that is executed when you run a setup for an application wherein the setup will save the application files on program files folder. I'm not trying to create a vb.net setup, because I want to run my program as portable. What I want the program to do is to place the files in their appropriate location when the user clicks a button
Here's my current code:
Public Class Nircmd
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'shutdown
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Shutdown.lnk")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'monitor off
System.Diagnostics.Process.Start("E:\Documents and Settings\Rew\Desktop\Monitor Off.lnk")
End Sub
End Class
-Of course it won't work if the path doesn't contain the file specified. So I want to place another button that would do just that(to save the files specified in the desired folder.
A simple syntax will do. Please
I don't quite understand, but I'll take a shot.
This will check to see if C:\foo\somefile.txt exists, and if it doesn't, create it and write some text:
If Not System.IO.File.Exists("C:\foo\somefile.txt") = True Then
Dim file As System.IO.FileStream
file = System.IO.File.Create("C:\foo\somefile.txt")
file.Close()
End If
My.Computer.FileSystem.WriteAllText("C:\foo\somefile.txt", "Some text")
If you want to copy or move a file, I think you'll want something like:
System.IO.File.Copy("C:\foo\somefile.txt", "C:\bar\somefile.txt")
or
System.IO.File.Move("C:\foo\somefile.txt", "C:\bar\somefile.txt")
I always use app.path in order to make it portable. Not everyone's computer will assign the same drive letter.