Here is code that works in normal folders but it does not work in the vault (solidworks epdm)
More info,if any file in any folder on my computer is selected(focused or highlighted) this code above works 100% .but I got a EPDM SolidWorks Vault folder on my C: Drive , in this vault folder the code above does not provide me with the selected item ,it gives me a blank value . No errors
Imports Shell32
Imports SHDocVw
Imports System.IO
Public Class Form1
Private Function GetExplorerSelectedFiles() As String()
Dim ExplorerFiles As New List(Of String)
Dim exShell As New Shell
For Each window As ShellBrowserWindow In DirectCast(exShell.Windows, IShellWindows)
If TryCast(window.Document, IShellFolderViewDual) IsNot Nothing Then
For Each fi As FolderItem In DirectCast(window.Document, IShellFolderViewDual).SelectedItems
ExplorerFiles.Add(fi.Name)
Next
ElseIf TryCast(window.Document, ShellFolderView) IsNot Nothing Then
For Each fi As FolderItem In DirectCast(window.Document, ShellFolderView).SelectedItems
ExplorerFiles.Add(fi.Name)
Next
End If
Next
Return ExplorerFiles.ToArray
End Function
Private Sub btntest_Click(sender As Object, e As EventArgs) Handles btntest.Click
Dim files = GetExplorerSelectedFiles()
Dim file As String = String.Join(".", files)
Label1.Text = file
End Sub
End Class
See "not working image" for more details
See "working image" for more details
Related
Using a button, I am attempting to allow a user to select a folder they wish to moves files from of only the following extensions: .shp, .dbf and .shx, to a folder that will be created upon moving these files with a set name (i.e. Exports).
EDIT*
Ok, here is what I have come up with:
Private Sub SelectFolder_Click(sender As Object, e As EventArgs) Handles SelectFolder.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
lstFiles(FolderBrowserDialog1.SelectedPath)
End If
End Sub
Private Sub ListFiles(ByVal folderPath As String)
lstFiles.Items.Clear()
Dim fileNames = My.Computer.FileSystem.GetFiles(
folderPath, FileIO.SearchOption.SearchTopLevelOnly, "*.shp", "*.shx", "*.dbf")
For Each fileName As String In fileNames
lstfiles.Items.Add(fileName)
Next
End Sub
Am I thinking about it this in the correct way?
Here's some tips:
To get specific file types from a directory, you can do:
Imports System.IO
'...
Dim tar = {".shp", ".shx", ".dbf"}
Dim files = Directory.EnumerateFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).
Where(Function(x) tar.Contains(Path.GetExtension(x).ToLower)).
Select(Function(x) New FileInfo(x))
Or by using the RegExp:
Imports System.IO
Imports System.Text.RegularExpressions
'...
Dim pattern = "^.*?(\.dbf|\.shp|\.shx)$"
Dim files = Directory.EnumerateFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly).
Where(Function(x) Regex.IsMatch(x, pattern, RegexOptions.IgnoreCase)).
Select(Function(x) New FileInfo(x))
To show the files in a ListBox:
lstFiles.DataSource = Nothing
'Or FullName to display the path.
lstFiles.DisplayMember = "Name"
lstFiles.DataSource = files.ToList
Now to copy these files to another folder and append a prefix/postfix:
Dim files = DirectCast(lstFiles.DataSource, List(Of FileInfo))
Dim dest As String = "DestinationPath"
Dim postfix As String = "Exports"
If Not Directory.Exists(dest) Then
Directory.CreateDirectory(dest)
End If
files.ForEach(Sub(x)
x.CopyTo(Path.Combine(dest,
$"{Path.GetFileNameWithoutExtension(x.Name)}_{postfix}{x.Extension}"),
True)
End Sub)
That's it all.
First a little background information: The purpose of this application is to capture images and save them automatically to a network directory that will be either created or appended using the input of the text box. This code DOES work on my computer (windows 7 home 64 bit). I've created it using microsoft visual basic express 2010.
However..... when attempting to run the application on a windows 10 tablet, I get the follow errors:
On form load:
An error occurred while capturing the image. The video capture will now be terminated.
Object reference not set to an instance of an object.
On button2_Click Event:
Object reference not set to an instance of an object.
Below is the entirety of the code.
Form2.vb
Public Class Form2
Public scanIsSet As Boolean
Private webcam As WebCam
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
webcam = New WebCam()
webcam.InitializeWebCam(imgVideo)
webcam.Start()
scanIsSet = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim CFGfile As String
Dim SaveDir As String
Dim imgIndex As Integer
Dim existingImages() As String
SaveDir = "C:\somepath\"
'save image to directory with index number
Try
imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg")
Catch ex As Exception
MsgBox("error while accessing object imgCapture" & ex.Message)
End Try
imgIndex = imgIndex + 1
Else
Beep()
MsgBox("Please scan or type in order number first")
End If
End Sub
End Class
WebCam.vb
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports WebCam_Capture
Imports System.Collections.Generic
Imports ZXing
Imports ZXing.OneD
'Design by Pongsakorn Poosankam
Class WebCam
Public scanz As Boolean
Public Sub setScan(ByVal x As Boolean)
scanz = x
End Sub
Private webcam As WebCamCapture
Private _FrameImage As System.Windows.Forms.PictureBox
Private FrameNumber As Integer = 30
Public Sub InitializeWebCam(ByRef ImageControl As System.Windows.Forms.PictureBox)
webcam = New WebCamCapture()
webcam.FrameNumber = CULng((0))
webcam.TimeToCapture_milliseconds = FrameNumber
AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured
_FrameImage = ImageControl
End Sub
Private Sub webcam_ImageCaptured(ByVal source As Object, ByVal e As WebcamEventArgs)
_FrameImage.Image = e.WebCamImage
If scanz = True Then
Dim BCreader As New ZXing.BarcodeReader
'BCreader.Options.TryHarder = True
Try
Dim resu As Result = BCreader.Decode(e.WebCamImage)
Form2.OrderNumber.Text = resu.Text
setScan(False)
Form2.Label2.Text = ""
Beep()
Catch ex As Exception
'do nothing
End Try
End If
End Sub
Public Sub Start()
webcam.TimeToCapture_milliseconds = FrameNumber
webcam.Start(0)
End Sub
Public Sub [Stop]()
webcam.[Stop]()
End Sub
Public Sub [Continue]()
' change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber
' resume the video capture from the stop
webcam.Start(Me.webcam.FrameNumber)
End Sub
Public Sub ResolutionSetting()
webcam.Config()
End Sub
Public Sub AdvanceSetting()
webcam.Config2()
End Sub
End Class
As you can see toward the end of Form2.vb, I've wrapped imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg") in a Try-Catch block because I suspect it's some sort of problems with the pictureBox objects. The try catch block does indeed catch the exception, but I still have no idea what the problem is, why it happens on the tablet and not the PC, or how to fix it.
I've found similar questions, but none with a solution I can make use of.
Since you are using a library, EasyWebCam, that is outdated and not compatible with Win10, I would suggest switching libraries. Other options out there:
DirectX.Capture
Windows.Media.Capture
I FOUND THE SOLUTION BUT I DON'T KNOW IF YOU NEED IT NOW ANYWAY THE PROBLEM IS IF YOU HAVE CHANGED THE PICTUREBOX NAME THEN IN REFENCES USE THE EXACT NAME YOU HAVE CHANGED TO. EXAMPLE IF I CHANGE ALL MY PICTUREBOX NAMES AS -
PictureBox_A1 , PictureBox_A2 ,... and so on then my refence should be as -
Dim r As DataRow
For Each r In t1.Rows
CType(Controls("PictureBox_" & r(2)), PictureBox).Image = bookedicon
Next
MY REFERENCE IS - "PictureBox_"
I have written a small piece of code (see below) to back-up files and folders on a USB drive to the local disk. The program works fine, however after encrypting the flash drive using BitLocker. I get the following error
I should also note that the drive is accessible through Windows explorer. thanks in advance.
Imports System
Imports System.IO
Module Module1
Sub Main()
If My.Computer.Name = UCase("My-Toshiba") Then
CopyDirectory(Directory.GetCurrentDirectory(), "C:\Users\me\Documents\USB_Backup")
End If
End Sub
Private Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
If Not System.IO.Directory.Exists(destinationPath) Then ' If the destination folder don't exist then create it
System.IO.Directory.CreateDirectory(destinationPath)
End If
Dim fileSystemInfo As System.IO.FileSystemInfo
For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
If TypeOf fileSystemInfo Is System.IO.FileInfo Then 'check whether its a file or a folder and take action accordingly
If Not System.IO.File.Exists(destinationFileName) Then
System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
Else
Dim destFileInfo As New FileInfo(destinationFileName)
If fileSystemInfo.LastWriteTime > destFileInfo.LastWriteTime Then
System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
End If
End If
Else
If Not System.IO.File.Exists(destinationFileName) Then
CopyDirectory(fileSystemInfo.FullName, destinationFileName) ' Recursively call the mothod to copy all the nested folders
Else
Dim destFileInfo As New FileInfo(destinationFileName)
If fileSystemInfo.LastWriteTime > destFileInfo.LastWriteTime Then
CopyDirectory(fileSystemInfo.FullName, destinationFileName)
End If
End If
End If
Next
End Sub
End Module
I am trying to figure out the code on Visual Basic after I have already extracted all files from a folder that was in a flash drive and put them in a folder on the computer. How could I have this program delete all the files that have not been modified from a previous date in the folder on the computer?
This is what I have so far:
Imports System.IO
Public Class frmExtractionator
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim sourceDirectory As String = "E:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "E:\FilesExtracted"
Try
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If(Not System.IO.Directory.Exists(archiveDirectory )) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFile As String In txtFiles
Dim fileName = currentFile.Substring(sourceDirectory.Length + 1)
File.Move(currentFile, Path.Combine(archiveDirectory, fileName))
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
End Class
Something like this will delete files that have not been modified since the given date.
Private Sub DeleteUnmodifiedFiles(directoryName As String, modificationThreshold As Date)
Dim folder As New DirectoryInfo(directoryName)
Dim wasModifiedSinceThreshold As Boolean
For Each file As FileInfo In folder.GetFiles
wasModifiedSinceThreshold = (file.LastWriteTime > modificationThreshold)
If (Not wasModifiedSinceThreshold) Then file.Delete()
Next
End Sub
To delete based on a number of days...
Private Sub DeleteUnmodifiedFiles(directoryName As String, modificationThresholdDays As Integer)
Dim folder As New DirectoryInfo(directoryName)
Dim thresholdDate As Date
Dim wasModifiedSinceThreshold As Boolean
For Each file As FileInfo In folder.GetFiles
thresholdDate = DateTime.Now().AddDays(-1 * modificationThresholdDays)
wasModifiedSinceThreshold = (file.LastWriteTime > thresholdDate)
If (Not wasModifiedSinceThreshold) Then file.Delete()
Next
End Sub
I am using following code to get directory info. it works well if I search topleveldirectory.
But when i search alldirectories, it reaches system level information and throws error.
Is there any way to avoid searching system level information folder?
Thanks
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim di As New DirectoryInfo("d:\"), i As Integer
Dim aryFiles() As FileInfo = di.GetFiles("*.doc", SearchOption.TopDirectoryOnly)
For i = LBound(aryFiles) To UBound(aryFiles)
MsgBox(aryFiles(i).FullName)
Next i
End Sub
End Class
This code should do the trick for you.
Imports System.IO
Module Module1
Sub Main()
Dim folders = New DirectoryInfo("D:\").GetDirectories
Dim files = New List(Of FileInfo)
For Each folder In From d In folders Where d.Name <> "System Volume Information"
files.AddRange(folder.GetFiles("*.doc", SearchOption.TopDirectoryOnly))
Next
For Each File In files
MsgBox(File.FullName)
Next
End Sub
End Module
I'm assuming your project is .NET 3.5 or higher. Notify me if the assumption is wrong.
Edit
Since you requested for it, I hacked together code to automatically skip inaccessible folders. I did not test the code extensively so I cannot guarantee it will be bug-free.
Imports System.IO
Module Module1
Sub Main()
Dim folders = GetAllSubFolders("D:\Alex\Music")
Dim files = New List(Of FileInfo)
For Each folder In folders
files.AddRange(folder.GetFiles("*.doc", SearchOption.TopDirectoryOnly))
Next
For Each File In files
Console.WriteLine(File.FullName)
Next
Console.ReadLine()
End Sub
Function GetAllSubFolders(ByVal path As String) As IEnumerable(Of DirectoryInfo)
Dim subFolders As New List(Of DirectoryInfo)
Try
subFolders.AddRange(New DirectoryInfo(path).GetDirectories())
Catch ex As Exception
'error handling code goes here'
End Try
Dim innerSubFolders As New List(Of DirectoryInfo)
For Each folder In subFolders
innerSubFolders.AddRange(GetAllSubFolders(folder.FullName))
Next
'add the inner sub folders'
subFolders.AddRange(innerSubFolders)
'return the directories'
Return subFolders
End Function
End Module