how to get the accessible folder names from a drive with vb.net (Visual Studio 2012 Express) - vb.net

I am trying to create a program for a friend that allows him to "sort out" and locate his files.
He has a couple of drives and many folders. What I would like to do for him is:
Have code that lists all of the active drives in a combo box. He can then select which drive he wants to search for any or just selected file types by extension.
Then he either selects "Everything" or a specific type (ie: Music)
Once a button is clicked, a list box fills with the full path to the file
Beginning this writing process, I have accomplished this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.Enabled = False
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
If d.IsReady Then ComboBox1.Items.Add(d)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex <> -1 Then
Try
For Each DName As String In My.Computer.FileSystem.GetDirectories(ComboBox1.Text, FileIO.SearchOption.SearchTopLevelOnly)
Dim LDN As Integer = Len(DName)
Dim FName As String = Strings.Mid(DName, 4, LDN - 3)
CheckedListBox1.Items.Add(FName, False)
Next
Catch ex As Exception
End Try
Else
Exit Sub
End If
End Sub
My first problem is that the GetDirectories process is getting directories I know that I do not have access to. I am trying to figure out how to prevent this. Can anyone help me.

Related

Prevent deleted to be search on searchbox

I want to prevent the deleted Employee information to occur on my search bar as seen on the first picture there are only 3 users but when I put some numbers, this deleted one appear the code I use is as follows: Thankyouuu!
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles empId.TextChanged
Using db As New [Emme_Subic_Transport_Corporation_Payroll].EmmeSubicEntities
Dim drivers = db.UserDetails.Where(Function(c) c.EmployeeID.Contains(empId.Text)).ToList
If drivers.Count() > 0 Then
db.UserDetails.Where(Function(c) c.isDeleted <> 1).Load()
' db.UserDetails.Load()
UserDetailBindingSource.DataSource = db.UserDetails.Local
Else
MsgBox("No Match Found!")
End If
End Using
End Sub
Include another condition in the where which filters out the deleted users. And run the query off the UI thread. Here is a simple way to run your query on a background thread, off the UI, and it will only run when 1 second passes without an additional keystroke in the textbox
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Static queryTimer As New System.Threading.Timer(AddressOf runQuery, Nothing, -1, -1)
queryTimer.Change(1000, -1)
End Sub
Private Sub runQuery(state As Object)
Dim employeeID As String
Me.Invoke(Sub() employeeID = empId.Text)
UserDetailBindingSource.DataSource = Nothing
Dim drivers As IEnumerable(Of UserDetails)
Using db As New [Emme_Subic_Transport_Corporation_Payroll].EmmeSubicEntities
drivers = db.UserDetails.Where(Function(c) c.EmployeeID.Contains(employeeID) AndAlso Not c.IsDeleted).ToList()
End Using
UserDetailBindingSource.DataSource = drivers
If Not drivers.Any() Then MsgBox("No Match Found!")
End Sub

Remove line from text file which listed in list box

I have a text file(its hosts file)
I can add lines(domains) to the file through a text box.
When i click the 'Save' button, inputted value(domain) saves to the text(hosts) file with a 127.0.0.1 prefix. At the same time it shows that in a list box too(while showing it in list box, it won't show the prefix(127.0.0.1). It'll only show the value got from the text field)
What i need now is that,
When i click and select an item from the list box and press 'Remove' button, it needs to be removed from both the list box and the text(hosts) file.
Also, while removing the selected item from the list box, the prefix(127.0.0.1) also should be removed from the file without leaving an empty line.
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles BtnCustomDomainremove.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
Dim delLine As Integer = 1
Dim lines As List(Of String) = System.IO.File.ReadAllLines("C:\Windows\System32\drivers\etc\hosts").ToList
lines.RemoveAt(delLine - 1)
System.IO.File.WriteAllLines("C:\Windows\System32\drivers\etc\hosts", lines)
MessageBox.Show("URL removed from Block List !", "Custom Block List")
End Sub
Above code it removes the top value from file and selected one from list box.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines As List(Of String) = System.IO.File.ReadAllLines("C:\Windows\System32\drivers\etc\hosts").ToList
Dim Findstring = IO.File.ReadAllText("C:\Windows\System32\drivers\etc\hosts")
Dim Lookfor As String = CStr("127.0.0.1" + ListBox1.SelectedItem)
If lines.Contains(Lookfor) Then
For i = 0 To Findstring.Length - 1
lines.Remove(Lookfor)
Next
System.IO.File.WriteAllLines("C:\Windows\System32\drivers\etc\hosts", lines)
ListBox1.Items.Remove(ListBox1.SelectedItem)
MessageBox.Show("URL removed from Block List !", "Custom Block List")
Else
MsgBox("string not found")
End If
End Sub
This code works for me, you might have to tweak it slightly to match your prefereneces however, it seems to find each occurunce (if there's more than one) of the selected item in listbox1 (if there's only one occurance you can remove the for loop.
Instead of Reading and Writing the file on every change to the list, save the list when the form closes. Read the file when the form opens and populate the list. While the form is open just add and remove list items as you normally would.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path = "C:\Windows\System32\drivers\etc\hosts.txt"
Dim lines() As String = File.ReadAllLines(path)
'Substring(9) the characters in "127.0.0.1" are index 0-8 so we begin with index 9
'and return the rest of the string.
Dim TrimmedList = From line In lines
Select line.Substring(9)
For Each item In TrimmedList
ListBox2.Items.Add(item)
Next
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim sb As New StringBuilder()
For Each item In ListBox2.Items
sb.AppendLine("127.0.0.1" & item.ToString)
Next
Dim path = "C:\Windows\System32\drivers\etc\hosts.txt"
File.WriteAllText(path, sb.ToString)
End Sub
Requires
Imports System.IO
Imports System.Text
try this code
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles BtnCustomDomainremove.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
Dim details As String = ""
For Each o As Object In ListBox1.Items
details += o.ToString + vbNewLine
Next
My.Computer.FileSystem.WriteAllText("C:\Windows\System32\drivers\etc\hosts", details, False)
MessageBox.Show("URL removed from Block List !", "Custom Block List")
End Sub

if the file not exits then return no error vb

I have start this application every things its work fine but i have a small bug but i can not find the solution to solve the error.
i have debug it and the error its because the file not exist
is there any way to me to populate my datagridview with all *.gif images From a directory and the check if its null or some thing like that.
What i mean is is there any way to my to populate all gif images found on the chose Directory?
in fact i have all ready try like this but i get one error "Provided column already belongs to the DataGridView control.
Well finaly i have found a solution to load all images from a directory to a datagridview programmatic
Here Is The Working Code
Public Class Form5
Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click
'Populate()
ShowImages()
End Sub
'CLEAR DATAGRIDVIEW
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
DataGridView1.Rows.Clear()
End Sub
'WHEN AN IMAGE IS CLICKED
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
MessageBox.Show("You Clicked Image At Col: " + e.ColumnIndex.ToString() + " Row: " + e.RowIndex.ToString())
End Sub
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub ShowImages()
Dim directory As New System.IO.DirectoryInfo("C:\avitogifconverter\")
If directory.Exists Then
Dim pngFiles() As System.IO.FileInfo = directory.GetFiles("*.gif")
For Each pngFile As System.IO.FileInfo In pngFiles
If pngFile.Exists Then
Dim image = System.Drawing.Image.FromFile(pngFile.FullName)
Using image
Dim count = 1
' do something with the image like show in picture box
'CONSTRUCT IMG COLUMN
Dim imgCol As DataGridViewImageColumn = New DataGridViewImageColumn()
imgCol.HeaderText = "Photo"
imgCol.Name = "Col 1"
DataGridView1.Columns.Add(imgCol)
'CONSTRUCT ROWS
'FIRST ROW
Dim img As Image = System.Drawing.Image.FromFile(pngFile.FullName)
Dim row As Object() = New Object() {img, img, img}
DataGridView1.Rows.Add(row)
End Using
End If
Next
End If
End Sub
End Class
Using Directory.EnumerateFiles, you could do something like this:
Dim row = New List(Of Image)(3)
For Each filename In Directory.EnumerateFiles("C:\avitogifconverter", "*.gif")
row.Add(Image.FromFile(filename))
If row.Count = 3 Then
DataGridView1.Rows.Add(row.ToArray())
row.Clear()
End If
Next
If row.Count > 0 Then
DataGridView1.Rows.Add(row.ToArray())
row.Clear()
End If

VB.NET How to copy the application's executable file to another path?

I'm new to VB.NET and I'm trying to experiment with it. So when the user clicks a button from the application, the executable file should copy itself to a specific path.
So, how can I do that?
Thanks.
This is not recommended but possible - think about why in the earth you want to do that. A software is not just an EXE.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileToCopy As String
Dim NewCopy As String
FileToCopy = Application.StartupPath.ToString & "\WindowsApplication1.exe"
NewCopy = "D:\_working\WindowsApplication1.exe.copied"
If System.IO.File.Exists(FileToCopy) = True Then
System.IO.File.Copy(FileToCopy, NewCopy)
MessageBox.Show("File Copied")
End If
End Sub
End Class
The code above is flawed if you are speaking about getting your own assembly in Visual Studios to copy itself to a given path. I tested the following code and it worked without error. Please note the path will have to be changed to fit your needs and be sure to use the name of your program, mine was CryLok.exe.
Reference: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.location?view=net-5.0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FileToCopy As String
Dim NewCopy As String
FileToCopy = Reflection.Assembly.GetExecutingAssembly().Location
NewCopy = "C:\Users\your name\Desktop\CryLok.exe"
If File.Exists(FileToCopy) = True Then
File.Copy(FileToCopy, NewCopy)
MessageBox.Show("File Copied")
End If
End Sub

Visual Studio rename files on a directory with cycle for and substitute it with text from a textbox

I need help with the cycle for, when program start should rename files on selected directory but it doesn't.
This is the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Label4.Text = FolderBrowserDialog1.SelectedPath
Dim counter = FolderBrowserDialog1.SelectedPath
Label5.Text = counter.Count
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim basedir As String = FolderBrowserDialog1.SelectedPath
For counter As Integer = 0 To Int(Label5.Text)
My.Computer.FileSystem.RenameFile(basedir, TextBox1.Text + "x")
Next
End Sub
End Class
First of all, FolderBrowserDialog1.SelectedPath returns a normal string. Calling SelectedPath.Count will not give you how many files are in the directory, but how many character the string path consists of.
Secondly, calling RenameFile(basedir, ...) won't do anything since basedir points to a single directory - not the files in the directory.
In order to get a proper file count, AND iterate through the file names you'd have to use something like Directory.GetFiles(basedir).
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Label4.Text = FolderBrowserDialog1.SelectedPath
Label5.Text = Directory.GetFiles(FolderBrowserDialog1.SelectedPath).Count
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim basedir As String = FolderBrowserDialog1.SelectedPath
Dim Files As String() = Directory.GetFiles(basedir) 'Declare an array that holds all file paths.
For counter As Integer = 0 To Files.Length - 1 'Minus one is important here, so that you won't get a "IndexOutOfRangeException".
My.Computer.FileSystem.RenameFile(Files(counter), TextBox1.Text & counter)
Next
End Sub
Files(counter) represents the file that it's currently at. If counter = 0 then Files(counter) would be the first file path in the array, and so on.
One thing I don't understand is why you give the new files the name of whatever you put in TextBox1 and an x. You are currently giving every file in that directory the exact same name (which is impossible). Could you tell me what you really are trying to rename them to?
In the mean time I replaced the x with counter. That will rename them to <whatever is in TextBox1>0, <whatever is in TextBox1>1, and so on.
One last thing, as you might have noticed I put the ampersand (&) instead of the plus sign (+) in the second parameter of RenameFile. Using the ampersand is the recommended way of concatenating strings, as the plus sign could cause exceptions in certain cases.
EDIT:
To keep the extension of the file you just have to extract it, and then add it to the new name:
For counter As Integer = 0 To Files.Length - 1
Dim FileExtension As String = Path.GetExtension(Files(counter)) 'Keeping the extension.
My.Computer.FileSystem.RenameFile(Files(counter), TextBox1.Text & counter & FileExtension)
Next