vb.net get contextmenustrip applicable to multiple pictureboxes - vb.net

In my form I have several pictureboxes and one contextmenustrip, the contextmenustrip is supposed to use at all these pictureboxes.
A tool in the contextmenustrip is to open and view the a pdf file.
The current code is:
Private Sub ViewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewToolStripMenuItem.Click
Process.Start("C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe", "C:\vb_test\" + picDrawing(1))
End Sub
I have no idea how the tool can determine which picturebox is focused and open different files.
I am using vb.net.

It seems that ContextMenu.SourceControl returns always Nothing in particular situations. I verified this problem on VS2010 when I put my ToolStripMenuItem inside a ToolStripDropDownMenu. So the answer posted by #Justin Ryan couldn't working.
A workaround could be manually set a variable when opening ContextMenu with its SourceControl.
Public Class Form1
Dim ctrlSourceControl As Control
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddHandler Me.ContextMenuStrip1.Opening, AddressOf setSourceControl
End Sub
Private Sub setSourceControl(sender As Object, e As System.ComponentModel.CancelEventArgs)
Me.ctrlSourceControl = CType(sender, ContextMenuStrip).SourceControl
End Sub
Private Sub Item1ToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles Item1ToolStripMenuItem.Click
MsgBox(Me.ctrlSourceControl.Name)
End Sub
End Class

From this question, Tim Lentine shows how to use the SourceControl property Of a ContextMenuStrip to determine the control which opened the context menu:
Private Sub mnuWebCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuWebCopy.Click
Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)
MessageBox.Show(cms.SourceControl.Name)
End Sub

This answer is likely the same as tezzo's. Just used to this
scenario a lot, and wanted to share some security checks among many
others that may help.
Assuming
ONLY PictureBoxes can show your ContextMenu named MyContextMenu :
and you have a ToolStripMenuItem named ViewToolStripMenuItem
Declarations :
Option Strict On
Option Explicit On
Option Infer Off
Imports System.IO
' File.Exist()
Imports System.Diagnostics
' Process.Start()
Public Class Form1
Private p_SelectedPictureB As PictureBox
Private p_AcroRedPath As String = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"
Private p_ImageFolderPath As String = "C:\vb_test\"
Private p_TargetFileName As String = ""
' ...
End Class
Handle the Opening Event of your MyContextMenu [ContextMenu]
either by a Handles MyContextMenu.Opening hook
or AddHandler MyContextMenu.Opening, AddressOf MyContextMenu_Opening
Private Sub MyContextMenu_Opening( _
sender As Object, e As System.ComponentModel.CancelEventArgs) _
Handles MyContextMenu.Opening ' Requires Private WithEvents MyContextMenu
'Try
p_SelectedPictureB = DirectCast(MyContextMenu.SourceControl, PictureBox)
If p_SelectedPictureB.Image IsNot Nothing Then
ViewToolStripMenuItem.Enabled = True
Select Case True
Case p_SelectedPictureB Is PictureBox1:
p_TargetFileName = picDrawing(1)
ViewToolStripMenuItem.Text = "Open [" + p_TargetFileName + "]"
Case p_SelectedPictureB Is PictureBox2:
p_TargetFileName = picDrawing(2)
ViewToolStripMenuItem.Text = "Open [" + p_TargetFileName + "]"
' ...
Case Else
ViewToolStripMenuItem.Enabled = False
ViewToolStripMenuItem.Text = "Open [<Wrong PBox>]"
p_TargetFileName = ""
'e.Cancel = True
End Select
Else
ViewToolStripMenuItem.Enabled = False
ViewToolStripMenuItem.Text = "Open [<No Image>]"
p_TargetFileName = ""
'e.Cancel = True
End If
'Catch CurrentException As Exception
' MessageBox.Show(CurrentException.Message)
' ViewToolStripMenuItem.Enabled = False
' p_TargetFileName = ""
' e.Cancel = True
'End Try
' ^^ remove commenting if you're unsure.
End Sub
Prefer the use of top declared variables.
If you write plain file/folder paths inside a method or function, you could loose track of it quickly, and some time later, you don't understand why your application suddenly started to crash (because the file went deleted/application uninstalled, or the folder renamed)
=> Put path to File/Folder in a global variable
However, the best move is to retrieve that path at Runtime, and ensure the existence of the File/Folder before going further...
Private Sub ViewToolStripMenuItem_Click( _
sender As Object, e As EventArgs) _
Handles ViewToolStripMenuItem.Click
Dim TargetFile As String = p_ImageFolderPath + p_TargetFileName
If File.Exists(TargetFile) Then
' (p_AcroRedPath existence checked when application starts)
Process.Start(p_AcroRedPath, TargetFile)
Else
MessageBox.Show("The File [" + TargetFile + "] doesn't exist.")
End If
End Sub

Related

How to download multiple files in list box using vb.net

I want to download multiples files and the download links in list box, Is there any way to do that using WebClient, i saw people do that by download file by file by it look difficult way and i want to show current downloading speed ,current file size and progress of total process
I solved this proplem by that way:
Imports System.Net
Public Class Form1
Private WithEvents DonloadFile As WebClient
Dim stopwatch As Stopwatch = New Stopwatch
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.SelectedIndex = 0
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
stopwatch.Start()
DonloadFile = New WebClient
Try
DonloadFile.DownloadFileAsync(New Uri(ListBox1.SelectedItem.ToString), "C:\Users\" & Environment.UserName & "\Desktop\" & IO.Path.GetFileName(ListBox1.SelectedItem.ToString))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub DonloadFile_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles DonloadFile.DownloadProgressChanged
Dim y As Integer = (e.BytesReceived / 1024) / 1024
Label6.Text = "Download Speed: " & String.Format("{0} MB/s", (y / (stopwatch.Elapsed.TotalSeconds)).ToString("0.00"))
Label2.Text = "Current File Size: " & Math.Round(e.TotalBytesToReceive / (1024 * 1024), 1) & " MB"
Label3.Text = "Current File Persent: " & e.ProgressPercentage
Label1.Text = "Total Files: " & ListBox1.Items.Count.ToString()
If ListBox1.Items.Count.ToString > (ListBox1.SelectedIndex + 1) Then
If e.ProgressPercentage = 100 Then
ListBox1.SelectedIndex += 1
End If
Else
MessageBox.Show("Download completed successful", "Info")
End If
End Sub
Private Sub DonloadFile_DownloadDataCompleted(sender As Object, e As DownloadDataCompletedEventArgs) Handles DonloadFile.DownloadDataCompleted
stopwatch.Reset()
End Sub
End Class
thanks for #Fawlty
Here is some code I wrote to demonstrate how you could do this. I created a FileDownloader class as this will allow you to get the source url and destination filename when the download completes. I have also added an event for progress. The problem with just using the WebClient class on its own is that you lose track of which file is being processed in the events.
Imports System.ComponentModel
Imports System.Net
' A class for downloading files, with progress and finished events
Public Class FileDownloader
' Private storage
Private FileURL As String = ""
Private SaveToPath As String = ""
' A class to return our result in
Public Class FileDownloaderFileDownloadedEventArgs
Public Property DownloadedURL As String
Public Property SaveToPath As String
Public Sub New(DownloadedURL As String, SaveToPath As String)
Me.DownloadedURL = DownloadedURL
Me.SaveToPath = SaveToPath
End Sub
End Class
' A class to show progress
Public Class FileDownloaderProgressEventArgs
Public Property DownloadURL As String
Public Property SaveToPath As String
Public Property TotalBytes As Long
Public Property ProgressBytes As Long
Public Property ProgressPercent As Integer
Public Property UserState As Object
Public Sub New(DownloadURL As String, SaveToPath As String, TotalBytes As Long, ProgressBytes As Long, ProgressPercent As Integer, UserState As Object)
Me.DownloadURL = DownloadURL
Me.SaveToPath = SaveToPath
Me.TotalBytes = TotalBytes
Me.ProgressBytes = ProgressBytes
Me.ProgressPercent = ProgressPercent
Me.UserState = UserState
End Sub
End Class
' The event to raise when the file is downloaded
Public Event FileDownloaded(sender As Object, e As FileDownloaderFileDownloadedEventArgs)
' The event to raise when there is progress
Public Event Progress(sender As Object, e As FileDownloaderProgressEventArgs)
' Pass in the URL and FilePath when creating the downloader object
Public Sub New(FileURL As String, SaveToPath As String)
Me.FileURL = FileURL
Me.SaveToPath = SaveToPath
End Sub
' Call Download() to do the work
Public Sub Download()
Using wc As New WebClient
AddHandler wc.DownloadFileCompleted, AddressOf DownloadFileCompleted
AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged
wc.DownloadFileAsync(New Uri(FileURL), SaveToPath)
End Using
End Sub
' Catch the download complete and raise our event
Private Sub DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
RaiseEvent Progress(Me, New FileDownloaderProgressEventArgs(FileURL, SaveToPath, e.TotalBytesToReceive, e.BytesReceived, e.ProgressPercentage, e.UserState))
End Sub
Private Sub DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs)
' Some code you want to run after each file has downloaded
RaiseEvent FileDownloaded(Me, New FileDownloaderFileDownloadedEventArgs(FileURL, SaveToPath))
End Sub
End Class
#Region "How to use the FileDownloader class"
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Loop the URLs in the ListBox
For Each item In ListBox1.Items
' Create a FileDownloader object to handler each one
Dim FD = New FileDownloader(item, "c:\temp\" & IO.Path.GetFileName(item))
' Attach the event handlers
AddHandler FD.FileDownloaded, AddressOf FileDownloaded
AddHandler FD.Progress, AddressOf Progress
' Start the download
FD.Download()
Next
End Sub
' Event handler for file download completed
Private Sub FileDownloaded(sender As Object, e As FileDownloader.FileDownloaderFileDownloadedEventArgs)
tb_Log.AppendText(e.DownloadedURL & " Downloaded To: " & e.SaveToPath & vbCrLf)
End Sub
' Event handler for progress
Private Sub Progress(sender As Object, e As FileDownloader.FileDownloaderProgressEventArgs)
tb_Log.AppendText(IO.Path.GetFileName(e.DownloadURL) & " " & e.ProgressPercent & "% downloaded" & vbCrLf)
End Sub
#End Region

VB.NET filecopy program Error : System.NullReferenceException in FolderBrowserDialog1

I am trying to make a program which copies files from one folder to another and I am also using a progress bar to see what files are copying. When I am running this program I am getting an error on folderbrowserdialog1. The error is System.NullReferenceException. When I am running the program I am able to open the application but when i select the button it gives me an error.
Public Class Form1
Dim my As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess
Dim directoryTargetLocation As String 'Selected file path
Dim Destinydirectory As String 'Selected dest directory path
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As Object = Nothing
FolderBrowserDialog1.Description = "Select directory" **// Gettig error in this Line**
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
directoryTargetLocation = .SelectedPath
TextBox1.Text = directoryTargetLocation.ToString
Button2.Enabled = True
End If
End With
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
FolderBrowserDialog2.Description = "Select destiny directory"
With FolderBrowserDialog2
If .ShowDialog() = DialogResult.OK Then
Destinydirectory = .SelectedPath
TextBox2.Text = Destinydirectory.ToString
TextBox2.Text = TextBox2.Text.Remove(TextBox2.Text.Length - 1) &
TextBox1.Text.Substring(TextBox1.Text.LastIndexOf("\"))
Button3.Enabled = True
End If
End With
End Sub
You declared FolderBrowserDialog1 as an object.
What you want to do is to declare it as a FolderBrowserDialog.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As New FolderBrowserDialog
FolderBrowserDialog1.Description = "Select directory"
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
directoryTargetLocation = .SelectedPath
TextBox1.Text = directoryTargetLocation.ToString
Button2.Enabled = True
End If
End With
End Sub
You have not created a FolderBrowserDialog1 object.
Edit
You were close!
What you need is:
Dim FolderBrowserDialog1 As New FolderBrowserDialog
This creates a new FolderBrowserDialog object.
What you have is:
Dim FolderBrowserDialog1 As Object = Nothing
Which creates a new generic object name FolderBrowserDialog1, and explicitly sets it to be "nothing"
This means it does not have any of the properties or methods you were referencing, so you get the error.

VB.NET: Code runs without error, but does not add item to listbox

I've got this embedded CMD on my form which I created using another person's code and everything works right. Inside one of the Private Subs (that seems to run every time a new line is written in the CMD output textbox), I've got a line which adds a item to a listbox (listboxs name is txtPlayerList) on another form labelled Status.
When this area of the code runs, it doesn't throw up any errors (and if I put a msgbox() on the same line, the msgbox() works). If I put the add to listbox line on form_load it works perfectly?
Here is my code, I've included everything from that form just in case (it is in the third sub from the top with the asterisks and comment "Get players and maybe other stuff as well"
Imports System.IO
Public Class Console
Public WithEvents MyProcess As Process
Private Delegate Sub AppendOutputTextDelegate(ByVal text As String)
Public LastLine As String
Public LastLineFormatted As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim LocalpathParent As String = Application.StartupPath() + "\MCserver"
'loads embed cmd
Me.AcceptButton = ExecuteButton
MyProcess = New Process
With MyProcess.StartInfo
.FileName = "CMD.EXE"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.WorkingDirectory = LocalpathParent
End With
MyProcess.Start()
MyProcess.BeginErrorReadLine()
MyProcess.BeginOutputReadLine()
AppendOutputText("Process Started at: " & MyProcess.StartTime.ToString)
'Resize with parent mdi container. Needs to be anchored & StartPosition = manual in properties
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived
AppendOutputText(vbCrLf & "Error: " & e.Data)
End Sub
Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
AppendOutputText(vbCrLf & e.Data)
'*****************************************
'Get Players and maybe other stuff as well
'*****************************************
LastLine = Me.OutputTextBox.Lines.Last
If Status.ServerStarted = True Then
If Me.LastLine.Contains(" joined the game") Then
LastLineFormatted = Me.LastLine
LastLineFormatted = LastLineFormatted.Replace(" joined the game", "")
'***THIS LINE BELOW WORKS IN FORM LOAD, BUT NOT HERE FOR SOME REASON???***
Status.txtPlayersList.Items.Add(LastLineFormatted)
MsgBox("add lastlineformatted")
ElseIf Me.LastLine.Contains(" left the game") Then
LastLineFormatted = Me.LastLine
LastLineFormatted = LastLineFormatted.Replace(" left the game", "")
Status.txtPlayersList.Items.Remove(LastLineFormatted)
End If
End If
End Sub
Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click
MyProcess.StandardInput.WriteLine(InputTextBox.Text)
MyProcess.StandardInput.Flush()
InputTextBox.Text = ""
End Sub
Private Sub AppendOutputText(ByVal text As String)
If OutputTextBox.InvokeRequired Then
Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
Try
Me.Invoke(myDelegate, text)
Catch
End Try
Else
Try
OutputTextBox.AppendText(text)
Catch
End Try
End If
End Sub
End Class
EDIT: Below is the code I have for form1 per request
'code
Public Class Form1
Public Localpath As String
Public Downloadpath As String
Public LocalpathParent As String
'when this form is closing, send stop to console to make sure it has closed and saved
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Console.MyProcess.StandardInput.WriteLine("stop") 'send an EXIT command to the Command Prompt
Application.Exit()
End
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'load stuff in background n stuff
Me.Show()
Me.Focus()
Configure.Show()
Configure.Hide()
Status.Show()
Status.Hide()
Console.Show()
Console.Hide()
End Sub
'CONSOLE.form
Private Sub ConsoleToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ConsoleToolStripMenuItem1.Click
'Hide all forms
Status.Hide()
Configure.Hide()
'Shown Form that you want to load
Console.Opacity = 100
Console.Show()
WindowState = FormWindowState.Normal
Console.MdiParent = Me
Console.OutputTextBox.SelectionStart = Console.OutputTextBox.Text.Length
Console.OutputTextBox.ScrollToCaret()
End Sub
'STATUS.form
Private Sub StatusToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles StatusToolStripMenuItem1.Click
'hide all forms
Console.Hide()
Configure.Hide()
'Show Form that you want to load
Status.Opacity = 100
Status.Show()
WindowState = FormWindowState.Maximized
Configure.Size = Me.Size
Status.MdiParent = Me
End Sub
'CONFIGURE.form
Private Sub ConfigurationToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ConfigurationToolStripMenuItem1.Click
'hide all forms
Status.Hide()
Console.Hide()
'Show form that you want to load
Configure.Opacity = 100
Configure.Show()
WindowState = FormWindowState.Maximized
Configure.Size = Me.Size
Configure.MdiParent = Me
End Sub
End Class
'code
It seems that your original code to create an embeded CMD window was interfering with the code to update the listbox in another mdi child. After finding another way to embed a cmd console, and some fiddling around, It seems to be working Ok. I haven't been able to test pure server output yet though.
THere have been quite a few changes to the code that are too big to post here, but the Alternative embedded CMD is this.
Place this in general form declarations
'command prompt variables
Private strResults As String
Private intStop As Integer
Private swWriter As System.IO.StreamWriter
Friend thrdCMD As System.Threading.Thread
Private Delegate Sub cmdUpdate()
Private uFin As New cmdUpdate(AddressOf UpdateText)
Public WithEvents procCMDWin As New Process
This in your form_load Sub
thrdCMD = New System.Threading.Thread(AddressOf Prompt)
thrdCMD.IsBackground = True
thrdCMD.Start()
and these declarations within your form Class
Private Sub Prompt()
AddHandler procCMDWin.OutputDataReceived, AddressOf CMDOutput
AddHandler procCMDWin.ErrorDataReceived, AddressOf CMDOutput
procCMDWin.StartInfo.RedirectStandardOutput = True
procCMDWin.StartInfo.RedirectStandardInput = True
procCMDWin.StartInfo.CreateNoWindow = True
procCMDWin.StartInfo.UseShellExecute = False
procCMDWin.StartInfo.FileName = "cmd.exe"
procCMDWin.StartInfo.WorkingDirectory = LocalpathParent
procCMDWin.Start()
procCMDWin.BeginOutputReadLine()
swWriter = procCMDWin.StandardInput
Do Until (procCMDWin.HasExited)
Loop
procCMDWin.Dispose()
End Sub
Private Sub UpdateText()
OutputTextBox.Text += strResults
OutputTextBox.SelectionStart = OutputTextBox.TextLength - 1
InputTextBox.Focus()
intStop = OutputTextBox.SelectionStart
OutputTextBox.ScrollToCaret()
If OutputTextBox.Lines.Count > 2 Then
LastLine = OutputTextBox.Lines.ElementAt(OutputTextBox.Lines.Count - 2)
If Status.ServerStarted = True Then
'get element 1 of split
If LastLine.Contains(" joined the game") Then
LastLineFormatted = ExtractName(LastLine, " joined the game")
'If listlineformatted.contains(Players.allitems) then do
Status.txtPlayersList.Items.Add(LastLineFormatted)
Status.Show()
ElseIf Me.LastLine.Contains(" left the game") Then
LastLineFormatted = ExtractName(LastLine, " left the game")
'If listlineformatted.contains(Players.allitems) then do
Status.txtPlayersList.Items.Remove(LastLineFormatted)
MsgBox("remove lastlineformatted")
End If
End If
End If
End Sub
Private Function ExtractName(unformattedString As String, stringToRemove As String) As String
Dim temp As String = Split(unformattedString, "Server]")(1).ToString
ExtractName = temp.Replace(stringToRemove, "")
End Function
Private Sub CMDOutput(ByVal Sender As Object, ByVal OutputLine As DataReceivedEventArgs)
strResults = OutputLine.Data & Environment.NewLine
Invoke(uFin)
End Sub

VB.Net FileSystemWatcher - Multiple Folders and Multiple Files

I have a requirement of monitoring a root folder under which there are multiple folders and each folder containing multiple files (.csv) These files are being copied to a workstation from a server using some other third party tool. I have to monitor the workstation root's folder and as an when a .csv file is created or updated in any of the child folders, I have to write the .csv file data (updated or new) to a sql server database. While writing the data to the sql server, I need to capture the child folder name in which the .csv file was updated/created. This way I can have separate folder's data in one database file.
While running the program on the workstation pc, the application crashes without any specific error message. And since the app is run on a remote pc, I cannot debug the app to check which line is causing the crash. Below is the code:
Public Class FolderWatchAndProcess
Dim blnMonitorUpdated As Boolean = True
Dim blnMonitorCreated As Boolean = True
Dim blnShowMessage As Boolean
Dim strFullFileName As String
Dim strFileName As String
Dim strFolderName As String
Private Delegate Sub updateLabel(ByVal newLabel As String)
Private Sub updateLabelHandler(ByVal labelText As String)
lblMetNo.Text = labelText
End Sub
Private Sub ReadAndInsertCSVData()
' This routine checks for duplicates and inserts new records into the sql server database
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'lblMonitorPath.Text = "C:\RootFolder\"
'btnStartWatching_Click(Nothing, Nothing)
'Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub btnStartWatching_Click(sender As Object, e As EventArgs) Handles btnStartWatching.Click
btnStartWatching.Text = "Stop Watching"
Dim aFileWatcherInstance As ArrayList = New ArrayList
For Each sMonitorFolder As String In lstFolders.Items
If IO.Directory.Exists(sMonitorFolder) Then
Dim oFileWatcher As FileSystemWatcher = New FileSystemWatcher
oFileWatcher.Path = sMonitorFolder
oFileWatcher.Filter = "*.CSV"
If blnMonitorUpdated Then
AddHandler oFileWatcher.Changed, AddressOf Me.FileSystemWatcherUpdated
Else
'RemoveHandler , AddressOf Me.FileSystemWatcherUpdated
End If
If blnMonitorCreated Then
AddHandler oFileWatcher.Created, AddressOf Me.FileSystemWatcherCreated
Else
'RemoveHandler , AddressOf Me.FileSystemWatcherCreated
End If
oFileWatcher.EnableRaisingEvents = True
aFileWatcherInstance.Add(oFileWatcher)
End If
Next
End Sub
Private Sub FileSystemWatcherCreated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
strFullFileName = e.FullPath
strFileName = Mid(e.Name, 3, Len(e.Name))
strFolderName = strFileName.Split("_")(0)
If Me.InvokeRequired Then
Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.BeginInvoke(d, New Object() {strFolderName})
Else
updateLabelHandler(strFolderName)
End If
ReadAndInsertCSVData()
End Sub
Private Sub FileSystemWatcherDeleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
'A file has been deleted from the child directory.
End Sub
Private Sub FileSystemWatcherUpdated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
strFullFileName = e.FullPath
strFileName = Mid(e.Name, 3, Len(e.Name))
strFolderName = strFileName.Split("_")(0)
If Me.InvokeRequired Then
Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.BeginInvoke(d, New Object() {strFolderName})
Else
updateLabelHandler(strFolderName)
End If
ReadAndInsertCSVData()
End Sub
End Class
Any help would be appreciated.
the Changed event will fire multiple times:
https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.110).aspx
probably you can use the IsFileLocked function that mention at here:
I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it?

Assistance with Copy & Paste VB System

The system is meant to work so that whenever the gta_sa process stops running, it copies the file selected when browsing cmdFile to cmdStorage's location. Problem I have right now is that it doesn't store the new file in .txt, but rather just as .file. It's openable, but not as default.
Also, I wasn't sure how to do the detection on when gtasa process was recently closed so I had to use if the process is active. I'd really appreciate it for some help, thanks.**
EDIT: Maybe it needs to use a timer? Not sure, thanks again.
Imports System.Diagnostics
Imports System Imports System.ComponentModel
Public Class frmChatLog
Dim ofdDone
Dim fldDone
Dim Completed
Private Sub cmdStorage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStorage.Click
Using fld As New FolderBrowserDialog()
If fld.ShowDialog() = Windows.Forms.DialogResult.OK Then
MessageBox.Show("Selected " & fld.SelectedPath)
fldDone = fld.SelectedPath
cmdStorage.Enabled = False
End If
End Using
End Sub
Private Sub cmdFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdFile.Click
Using ofd As New OpenFileDialog()
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
MessageBox.Show("Selected " & ofd.FileName)
ofdDone = ofd.FileName
cmdFile.Enabled = False
End If
End Using
End Sub
Private Sub cmdStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStart.Click
If cmdFile.Enabled = False And cmdStorage.Enabled = False Then
Do Until Process.GetProcessesByName("gta_sa").Count > 0
If Process.GetProcessesByName("gta_sa").Count > 0 Then
MsgBox("Game is on")
Else
System.IO.File.Move(ofdDone, fldDone & "\" & Today.Now.ToString("ddMMyyHHmmss"))
Completed = fldDone & "\" & Today.Now.ToString("ddMMyyHHmmss")
Completed.ChangeExtension(".txt")
Exit Do
End If
Loop
End If
End Sub
End Class
Problem I have right now is that it doesn't store the new file in .txt, but rather just as .file.
To fix that, change this line near the bottom of your code:
System.IO.File.Move(ofdDone, fldDone & "\" & Today.Now.ToString("ddMMyyHHmmss"))
to this:
File.Move(ofdDone, Path.Combine(fldDone, Now.ToString("ddMMyyHHmmss") & ".txt"))
Then get rid of any line that mentions your Completed variable. You may also need to add an Imports System.IO to the top of the file.