Deleting folders VB - vb.net

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.

Related

Download, get the filename and execute the file

I am posting here to ask you for help, thank you first of all for reading my message and being able to help me. :)
So here's the problem, I have a little program that I'm creating, in this one, I integrated a button that normally allows to download a tool to run it.
However the link of the file does not point directly in .exe it is a link like this one :
mydomain.com/file/file48.php
The problem is that I manage to get the file, but this one is named file48.php and gets the extension of a php file, what I would like is to get the original name of the downloaded .exe file in order to be able to execute it within my application.
Here is the current code I'm using :
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim client As New WebClient
AddHandler client.DownloadFileCompleted, AddressOf client_DownloadFileCompleted
client.DownloadFileAsync(New Uri("https ://mydomain.com/files/index.php"), fileName, filename)
End Sub
Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs)
Dim client = DirectCast(sender, WebClient)
client.Dispose()
Dim filename As String = CType(e.UserState, String)
End Sub
Thanks a lot for your help, I've been searching for hours without much success...

renaming files in a folder by other file names in another folder with vb.net

i'm writing a programme and i guess the title explain it all .
and it did work but with one file only , problem is the program ignores the next claarving the first file - the success - is still his priority and he can't find it so it crashes
this is my code
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
IO.Directory.SetCurrentDirectory(TextBox2.Text)
Dim info As New IO.DirectoryInfo(TextBox1.Text)
Dim imfo2 As New IO.DirectoryInfo(TextBox2.Text)
For Each fi In imfo2.GetFiles
For Each foo In info.GetFiles
Dim namewithoutavi As String = foo.Name.Substring(0, foo.Name.Length - 4)
Dim fixedname As String = namewithoutavi & ".srt"
My.Computer.FileSystem.RenameFile(fi.FullName, fixedname)
Next
Next
End Sub
can someone please tell me how to make it right ?
as you can see i'm a beginner and i'm really desperate for help :)

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

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

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.

Pressing a button in visual basic

I am new to Visual Basic.NET and I am just playing around with it. I have a book that tells me how to read from a file but not how to write to the file with a button click. All I have is a button and a textbox named fullNameBox. When I click the button it gives me an unhandled exception error. Here is my code:
Public Class Form1
Sub outputFile()
Dim oWrite As System.IO.StreamWriter
oWrite = System.IO.File.CreateText("C:\sample.txt")
oWrite.WriteLine(fullNameBox.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
outputFile()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Have you tried stepping through your application to see where the error is? With a quick glance, it looks like you might need to use System.IO.File on the fourth line (oWrite = IO.File...) instead of just IO, but I haven't tried to run it.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.FileName = ""
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName.Trim.Length <> 0 Then
Dim fs As New FileStream(SaveFileDialog1.FileName.Trim, FileMode.Create)
Dim sr As New StreamWriter(fs)
sr.Write(TextBox1.Text)
fs.Flush()
sr.Close()
fs.Close()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName.Trim.Length <> 0 Then
Dim fs As New FileStream(OpenFileDialog1.FileName.Trim, FileMode.Open)
Dim sw As New StreamReader(fs)
TextBox1.Text = sw.ReadToEnd
fs.Flush()
sw.Close()
fs.Close()
End If
End Sub
End Class
this is a complete functional program if you want, you just need to drag drop a textbox, openfiledialog, and a savefiledialog.
feel free to play around with the code.
enjoy
by the way, the problem in your code is that you "must" close filestream when your done using it, doing so will release any resource such as sockets and file handles.
The .net framework is a very powerful framework. In the same way (however) it has easy and convenient methods for simple tasks. Most individuals tend to complicate things in order to display knowledge. But less code = less processing = faster and more efficient application (sometimes) so the large above method may not be suitable. Along with that, the above mentioned method would be better off written as a sub or if returning something then a function.
My.Computer.FileSystem.WriteAllText("File As String", "TextAsString", Append as Boolean)
A general Example would be
My.Computer.FileSystem.WriteAllText("C:\text.text", "this is what I would like to add", False)
this is what I would like to add
can be changed to the current text of a field as well.
so a more specific example would be
My.Computer.FileSystem.WriteAllText("C:\text.text", fullNameBox.text, True)
If you would like to understand the append part of the code
By setting append = true you are allowing your application to write the text at the end of file, leaving the rest of the text already in the file intact.
By setting append = false you will be removing and replacing all the text in the existing file with the new text
If you don't feel like writing that part of the code (though it is small) you could create a sub to handle it, however that method would be slightly different, just for etiquette. functionality would remain similar. (Using StreamWriter)
Private Sub WriteText()
Dim objWriter As New System.IO.StreamWriter("file.txt", append as boolean)
objWriter.WriteLine(textboxname.Text)
objWriter.Close()
End Sub
The Specific Example would be
Private Sub WriteText()
Dim objWriter As New System.IO.StreamWriter("file.txt", False)
objWriter.WriteLine(fullnamebox.Text)
objWriter.Close()
End Sub
then under the button_click event call:
writetext()
You can take this a step further as well. If you would like to create a more advabced Sub to handle any textbox and file.
Lets say you plan on having multiple separate files and multiple fields for each file (though there is a MUCH cleaner more elegant method) you could create a function. {i'll explain the concept behind the function as thoroughly as possible for this example}
below is a more advanced sub demonstration for your above request
Private Sub WriteText(Filename As String, app As Boolean, text As String)
Dim objWriter As New System.IO.StreamWriter(Filename, app)
objWriter.WriteLine(text)
objWriter.Close()
End Sub
What this does is allows us to (on the same form - if you need it global we can discuss that another time, it's not much more complex at all) call the function and input the information as needed.
Sub Use -> General Sample
WriteText(Filename As String, app As Boolean)
Sub Use -> Specific Sample
WriteText("C:\text.txt, False, fullnamebox.text)
But the best part about this method is you can change that to be anything as you need it.
Let's say you have Two Buttons* and **Two Boxes you can have the button_event for the first button trigger the above code and the second button trigger a different code.
Example
WriteText("C:\text2.txt, False, halfnamebox.text)
The best part about creating your own functions and subs are Control I won't get into it, because it will be off topic, but you could check to be sure the textbox has text first before writing the file. This will protect the files integrity.
Hope this helps!
Richard Sites.