Search for file and retrieve it's path in VB - vb.net

Im trying to search for a file "prince.exe", and retrieve it's file path using VB.Net but I cant find a method of doing this. I figured you might be able to use the command prompt but I'm not sure how.
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Search for file and retrive path'
End Sub

Use the GetFiles function of System.IO.Directory. Make sure to use the overload that lets you pass the SearchOption value of AllDirectories.
Dim strStartingDirectory As String = "C:\YourStarting\Directory\"
Dim lstMatches As List(of string) = System.IO.Directory.GetFiles(strStartingDirectory, _
"prince.exe", System.IO.SearchOption.AllDirectories).ToList()

Related

Not read text file correct

I want when I read txt file and add in ListBox1.items, add this text http://prntscr.com/on12e0 correct text §eUltra §8[§716x§8].zip not like this http://prntscr.com/on11kv
My code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
My.Computer.FileSystem.CopyFile(
appDataFolder & "\.minecraft\logs\latest.log",
appDataFolder & "\.minecraft\logs\latestc.log")
Using reader As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(" Reloading ResourceManager: Default,") Then
Dim lastpart As String = line.Substring(line.LastIndexOf(", ") + 1)
ListBox1.Items.Add(lastpart)
End If
End While
End Using
My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")
End Sub
This question is only different from your first question in that your have substituted a ListBox for a RichTextBox. It seems you got perfectly acceptable answers to your first question. But I will try again.
First get the path to the file. I don't know why you are copying the file so I didn't.
Add Imports System.IO to the top of your file. The you can use the File class methods. File.ReadAllLines returns an array of strings.
Next use Linq to get the items you want. Don't update the user interface on each iteration of a loop. The invisible Linq loop just adds the items to an array. Then you can update the UI once with .AddRange.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\.minecraft\logs\latest.log")
Dim lines = File.ReadAllLines(appDataFolder)
Dim lstItems = (From l In lines
Where l.Contains(" Reloading ResourceManager: Default,")
Select l.Substring(l.LastIndexOf(", ") + 1)).ToArray
ListBox1.Items.AddRange(lstItems)
End Sub
If this answer and the previous 2 answer you got don't work, please lets us know why.

Error copying from resources

While copying from resources to a folder under appdata folder: i get an error, but I'm not finding any mistake in code..
Private Sub Help_Load(sender As Object, e As EventArgs) Handles MyBase.Load
File.WriteAllBytes(MainPath & "\Help.rtf", My.Resources.HelpRTF)
Dim HelpRTF = (MainPath & "\Help.rtf")
Helpbox.LoadFile(HelpRTF)
End Sub
HelpRTF is a .rtf file, MainPath is a directory under %appdata% folder
Error: Value of type 'String' cannot be converted to 'Byte()'.
Error at: My.Resources.HelpRTF
The reason why you get that error is because the second parameter of the File.WriteAllBytes() method takes a Byte(), not a String. If you want to write text (String) to a file, you can use the File.WriteAllText() method.
Since RTF's can contain images, text, etc. treating it as text can corrupt it and needless to say, encoding issues might occur. So, instead of using the File.WriteAllText() method, change the FileType of the HelpRTF resource to Binary instead of Text like this:
After that, you can use your code as it was:
Private Sub Help_Load(sender As Object, e As EventArgs) Handles MyBase.Load
File.WriteAllBytes(MainPath & "\Help.rtf", My.Resources.HelpRTF)
Dim HelpRTF = (SWinPath & "\Help.rtf")
Helpbox.LoadFile(HelpRTF)
End Sub
References:
File.WriteAllText - MSDN
File.WriteAllBytes - MSDN

How to save a txt file to a predetermined location in vb 2010?

Hi i have a textbox which displays a bunch of names on it. The names are within a string called "strNames". I'm trying to have a save button which saves the names as a txt file in a predetermined location. Here is the code for the save button. It creates the file but without the list of names. please help!
Given the fact that your strNames is an array of strings then you could use File.WriteAllLines, no need to use a StreamWriter here
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
File.WriteAllLines("C:\Test.txt", strNames)
End Sub
This has an advantage against the StreamWriter approach if you don't need particular processing to your input array before writing it to file, no foreach around the array strings and also you don't need to encapsulate the StreamWriter in a Using statement to ensure a proper release of the system resources
You need to write to the file and then call Close() on the StreamWriter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim w As IO.StreamWriter
w = New IO.StreamWriter("C:\Test.txt")
' strNames is an array so you have to iterate through the array and write each element
For Each item As String In strNames
w.WriteLine(item)
Next
w.Close()
End Sub
Make sure to use Try/Catch blocks in case any exception occurs during accessing and writing to the file.
Also, when you use resources such as Files, make sure to properly dispose of the object that's connected to the resource. Using the 'Using' statement is a great and safe way to do that. Here's an example of both in action.
Sub WriteToFile()
Dim strNames As String() = {"John", "Jimmy", "Joe"}
Try
Using oWriter As New IO.StreamWriter("C:\testFile.txt")
For Each strName As String In strNames
oWriter.WriteLine(strName)
Next
End Using
Catch ex As Exception
HandleException(ex)
End Try
End Sub

Visual Basic Form. How to let users save text file where they want

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim CurrentDir As String = Environment.CurrentDirectory
Dim OutputFile2 As String = IO.Path.Combine(CurrentDir, "input.txt")
IO.File.WriteAllLines(OutputFile2, Result1.Lines)
End Sub
Right now, I have coding that saves a text file in the current directory. However, I want to have a browse button for users so that they can pick where this text file is saved. How do I proceed this?
I was trying it by my self and I'm having a trouble with using save file dialog. If you can teach me how to use a save file dialog or anyway to write save browse button, I would very appreciate it!
The documentation for the SaveFileDialog object contains an example.
Here is a tutorial on how to implement SaveFileDialog using Toolbox in Visual Studio like you mentioned. The code sample is in C# but it can be easily converted to VB.
Link: www.dotnetperls.com/savefiledialog
Private Sub button1_Click(sender As Object, e As EventArgs)
' When user clicks button, show the dialog.
saveFileDialog1.ShowDialog()
End Sub
Private Sub saveFileDialog1_FileOk(sender As Object, e As CancelEventArgs)
' Get file name.
Dim name As String = saveFileDialog1.FileName
' Write to the file name selected.
' ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test")
End Sub

VB.NET Too many arguments to 'Public Shared Function Exists(path As String) As Boolean'

I cannot seem to figure this out, why am I getting this error (Error 1 Too many arguments to 'Public Shared Function Exists(path As String) As Boolean')?
Here is my code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim CopyFile As String
Dim CopyTo As String
CopyFile = src1.Text
CopyTo = dest1.Text
If System.IO.File.Exists(CopyFile) = True Then
System.IO.File.Exists(src1.Text, dest1.Text)
MsgBox("Copied!")
End If
End Sub
Thank you so much!
I guess you wanted to copy the file. So instead of
System.IO.File.Exists(src1.Text, dest1.Text)
you need
System.IO.File.Copy(src1.Text, dest1.Text)
There is no overload as File.Exists(string,string) for method see MSDN
As per my understanding, if you want to copy file Use File.Copy
System.IO.File.Copy(src1.Text, dest1.Text)