I have this vb code which detects when a pendrive is inserted. Can anyone help me to be able to access the content of the pendrive in my form?? Thank you
Use IO.Directory.GetDirectories() and IO.Directory.GetFiles() from the path of the pendrive. You can see the MSDN documentation here and here, respectively.
Improve your question in order to have more specific answers.
EDIT:
Adding the file names into a ListBox:
Dim allFiles() As String = Directory.GetFiles(penDrivePath)
For Each file As String in allFiles
ListBox1.Items.Add(file)
Next
Related
I found a similar question but it was 5 years 8 months old, had 2 replies and neither worked for me (VB.Net Read txt file from current directory)
My issue is that when I use the following code:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Username_And_Password_Raw.txt")
Dim usernameAndPassword = Split(fileReader, ",")
I get an error saying:
System.IO.FileNotFoundException: 'Could not find file 'C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\bin\Debug\net6.0-windows\Username_And_Password_Raw.txt'.'
I have tried using all the different Applications.BLANKPath options I can find (ie; StartupPath, CommonAppDataPath, etc.) and they all return essentially the same error only with a different location.
This is the folder layout of my TXT File - I know it's a terrible, incredibly insecure and generally awful way of storing login information but this is just for a NEA so will never ever actually be used
This is the actual path of the TXT File if it helps
C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\Username_And_Password_Raw.txt
The startup path is where your exe is located. That and all supporting files get copied to a binary directory when you compile in visual studio, in your case
C:\Users\wubsy\source\repos\NEA Stock Page System\NEA Stock Page System\bin\Debug\net6.0-windows
But what you're trying to do, reference the file where it sits in your solution, is probably not the best way to do it, and your code above will work (with a change, will mention later) if you change the properties of the file in the solution.
Right click on the file in the Solution Explorer Username_And_Password_Raw.txt, select Properties. Modify Copy to Output Directory to either Copy always / Copy if newer, depending on your requirement. Now that file will copy to the same directory your exe is in, and the code above should work.
Note, when creating a path, don't use string concatenation because you may have too many or too few \; use Path.Combine:
Dim filePath = Path.Combine(Application.StartupPath, "Username_And_Password_Raw.txt"
Dim fileContents = My.Computer.FileSystem.ReadAllText(filePath)
I have a code that allows me to use an open file dialog to select a file. Once selected, the file path displays in Form3. What I'm trying to do from there is click Command_Button1 (Open) and physically open the file.
I can provide codes and JPEGs...but won't cloud this up until it is necessary :) I'm not using Common Dialog so the code is somewhat lengthy.
I looked online for a few hours yesterday and the closest I got was how to get it to open an Excel file...but this isn't Excel.
P.S. I'm new to VBA...so if this isn't as simple as a few lines of code and I'm missing the difficulty of it, please let me know. It seems like it should be a simple enough process...
Edit 1:
#mehow, Do you think it could be because the whole path name isn't displaying? I double clicked Open and nothing happened like you said
If you already know the path then..
Private Sub CommandButton1_Click()
dim myPath as String
myPath = "C:\files\file1.exe"
Dim shell As Object
Set shell = CreateObject("Shell.Application")
shell.Open myPath
End Sub
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I made a explorer.exe clone, with a treeview and a listview etc.
Now I need to handle clicking a .lnk file, so I need the destination path of a .lnk file..
You can use a WshShell Object:
You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.
and use its CreateShortcut method:
Creates a new shortcut, or opens an existing shortcut.
The resulting WshShortcut object contains a TargetPath property, which is what you're looking for.
Example:
Dim shell = CreateObject("WScript.Shell")
Dim path = shell.CreateShortcut(path_to_your_link).TargetPath
I'd try;
Public Shared Function GetLnkTarget(lnkPath As String) As String
Dim shl = New Shell32.Shell()
' Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath)
Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath))
Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath))
Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject)
Return lnk.Target.Path
End Function
You need to reference a COM object; Microsoft Shell Controls And Automation.
How can you obtain a list of files (as a stringcollection or some other storage method) which contains the full path on the user's computer to the files?
Is there a method for doing so?
It looks like you want to use Directory.GetFiles() in the System.IO namespace.
Docs here.
Dim txtFiles = Directory.GetFiles("C:\Input", "*.CSV", SearchOption.TopDirectoryOnly).
[Select](Function(nm) Path.GetFileName(nm))
Dim arrayList As New System.Collections.ArrayList()
For Each filenm As String In txtFiles
arrayList.Add(New clsImportFiles(filenm))
Next
Add a Listbox to Windows Form and Add following code on Form Load or other events :-
ListBox1.Items.AddRange(Directory.GetFiles("Your Directory PAth Here"))
Hope IT Helps ; From Nirav
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
I made a game and I would like to store the high score and other values in the windows registry. It's made in VB.NET. Could someone give me a sample code example of simple reading and writing to the registry.
Thanks
You must open Registry sub Key before read or write. Then you can read or write
Dim regKey As RegistryKey
Dim Value As Object
regKey =My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\Software\VB_and_VBA_Program_Settings", True)
'Here u can read value of AppName
Value = regKey.GetValue("AppName", "Default Value")
'Or u can write the value
value=regkey.setValue("AppName", "myApp")
regKey.Close()
Simply...
Imports Microsoft.VisualBasic
Dim s As String
SaveSetting("(AppName)", "(SectionName)", "(Key)", "(Your Value)")
s = GetSetting("(AppName)", "(SectionName)", "(Key)", "(Default Value)")
Replace (AppName), (SectionName), (Key) with appropriate values. The data will be saved in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\(AppName)
http://www.vbdotnetheaven.com/UploadFile/mahesh/WindowsRegistry04262005045814AM/WindowsRegistry.aspx
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx
I'm more comfortable with C#, but it's pretty straightforward with VB.NET too. Here's an example of how to write to the registry, and another example of how to read from the registry. Don't forget to import the Microsoft.Win32 namespace.
You can use registry.getvalue and registry.setvalue. Here are a couple of examples used for default file types:
Registry.GetValue("HKEY_CURRENT_USER\software\classes" & "\" & fileFormatExt(i), "", "error")
Registry.SetValue("HKEY_CURRENT_USER\software\classes\" & FileType, "", appTag) ' set new value, overwrite any other, creates key if not there.