Read .evtx file exported from another machine - vb.net

Using VB.net and the code below I can read the local security log. How do I read the events from an evtx file saved from another computer?
Dim elevent As New System.Diagnostics.EventLog("Security", Environment.MachineName)
Dim elEventEntry As System.Diagnostics.EventLogEntry
For Each elEventEntry In elevent.Entries
'Read the event information
Next

Related

vb.net check if file being fully copied before moving this file to backup directory

I have a video recording software that will record video files in directory. and I am writing vb.net code to move finished files into another directory ( external hard disk).
I only move files which have been finished recording.
I need only to move files which are finished recording. The problem is my code move files that are still being recorded.
my question : how I can check if a file is not being recorded right now ?
Thanks
In the following code you'll know best how long since the file was written indicates finished.
Dim di As New IO.DirectoryInfo(dirPath)
Dim noMod As New TimeSpan(0, 2, 0) 'last write time
Dim _movTM As DateTime = DateTime.Now.Add(-noMod)
For Each fi As IO.FileInfo In di.GetFiles
If fi.LastWriteTime < _movTM Then
'move here
End If
Next

Process cannot access it is being used by another process

I got solution with three separated windows services. One services download files, second is taking the files and third one is loading to db. Sometimes during downlading files, second process is starting to play with it during its still downloading, or third process wants to load it but file is still parsing. I heard that Mutex could be resolution in my case but i am not sure how in this situation i could use it the other processes waits on files untill they are "free". Below i'll present you my code:
Downlading process part of code where file download is doing:
(in this case each file is downloading to specific location folder)
For Each item In RemoteFiles
Try
session.GetFiles(item, DownloadFolder, True, transferOptions).Check()
Catch ex As Exception
End Try
Next
Parsing win service process part of code which is parsing downloaded files:
Dim ColumnFound As Boolean = False
Using MyReader As New FileIO.TextFieldParser(fileToBeParsedPath)
Dim CurrentRecord As String() ' this array will hold each line of data
With MyReader
.TextFieldType = FileIO.FieldType.Delimited
.Delimiters = New String() {","}
.HasFieldsEnclosedInQuotes = True
End With
After that process each file are migrated to ParsedFolder. But data from it is saved in files with .dat extension. And those files are used by my Loader engine.
How can i do some "lock on file" for instance for files which are during download process to prevent parser to take them under parsing process and wait? Can i make some centralized place where file status will be saved, so parser after take existing files into the list and then when is going to open and parse them will check and wait untill its free?? Please about your advice/code.
EDITED:
•Ftp two files but only watch one. For example send the files important.txt and important.finish. Only watch for the finish file but process the txt.
•FTP one file but rename it when done. For example send important.wait and have the sender rename it to important.txt when finished.

Loading Sequential Files in VB

I have written a vb program a few years ago and now as I get started with vb again i am hitting a "snag". With sequential files. I am trying to load a file in to the vb program with the file dialog box.
NOTE:I am using structures
Dim FileDialog as new openFileDialog
Dim MyStream as Stream = nothing
Dim FileLocation as string 'this is to save the file location
if( FileDialog.ShowDialog() = DialogResults.OK)Then
FS = new FileStream(FileLocation, FileMode.open, fileaccess.Read)
BF = new BinaryFromatter
While FS.Position < FS.Length
Dim temp as unit
...'Please note that this is where the file reads the structures data.It is to much code to write in.
When I run the program I can create a file and save it with the data in and I can load it with the Dialog box, The problem is when I run the program again and try to load it. It just wont run the file or load it(Remember I created the file with this program and saved)
How do I get this to work?
Make sure you have closed the file after writing and reading the data the first time, and make sure you are using the correct path (FileLocation).
Exit Visual Studio between the first and second times you run the program. If it works then, then you know you are not closing the file properly.
Set a breakpoint at the new FileStream assignment and check the value of FileLocation. Is it the same as it was when the file was written?
Check the error message, if there is one, and see if that tells you anything.

Let VB Form prepare working environment in chosen directory

I am working on a GUI for a simulation program. The simulation program is a single .exe which is driven by an input file (File.inp placed in the same directory).
The Original.inp file functions as a template from which the form reads all the values into an array. Then it changes these values reflecting the changes done by the user in the form. After that it writes all the new values to File.inp.
By pushing the "Run" button the Simulation.exe file is executed.
The folder structure looks like this:
root
|
|---input
| |
| |--Original.inp
|
|---GUI.exe
|---Simulation.exe
|---File.inp
Ideally I would supply only the GUI, the user would select the working directory and then the GUI.exe would create an input folder and extract the Original.inp and Simulation.exe in the appropriate locations. So far I have only managed to include Original.inp and Simulation.exe as "EmbeddedResources" in my VB project and I have let my code create an input folder in the working directory chosen by the user.
Can someone please explain to me how I can extract the .inp and .exe file into the correct directories? I've searched on google, tried File.WriteAllBytes and Filestream.WriteByte but did not get the desired results.
The problem with File.WriteAllBytes was that I could not point to the embedded resource ("Simulation.exe is not a member of Resources" and with Filestream.WriteByte I got a 0 kb file.
The question commenters are correct, this is probably a task best left for a setup program. However, that having been stated, in the interest of answering the question as asked I offer the following approach.
Contrary to your supposition in your question comment, you do need to "read" the embedded resource from the GUI's executable file, since it's an embedded resource and not an external resource. It won't magically extract itselt from the executable file. You need to do the manual read from the assembly and write to your specified locations. To do this, you need to read the resource using .Net Reflection, via the currently executing assembly's GetManifestResourceStream method.
The Simulation.exe file is a binary file so it must be handled as such. I assumed that the Orginal.inp file was a text file since it afforded the opportunity to demonstrate different types of file reads and writes. Any error handling (and there should be plenty) is omitted for brevity.
The code could look something like this:
Imports System.IO
Imports System.Reflection
Module Module1
Sub Main()
'Determine where the GUI executable is located and save for later use
Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim appFolder As String = Path.GetDirectoryName(thisAssembly.Location)
Dim fileContents As String = String.Empty
'Read the contents of the template file. It was assumed this is in text format so a
'StreamReader, adept at reading text files, was used to read the entire file into a string
'N.B. The namespace that prefixes the file name in the next line is CRITICAL. An embedded resource
'is placed in the executable with the namespace noted in the project file, so it must be
'dereferenced in the same manner.
Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Original.inp")
If fileStream IsNot Nothing Then
Using textStreamReader As New StreamReader(fileStream)
fileContents = textStreamReader.ReadToEnd()
textStreamReader.Close()
End Using
fileStream.Close()
End If
End Using
'Create the "input" subfolder if it doesn't already exist
Dim inputFolder As String = Path.Combine(appFolder, "input")
If Not Directory.Exists(inputFolder) Then
Directory.CreateDirectory(inputFolder)
End If
'Write the contents of the resource read above to the input sub-folder
Using writer As New StreamWriter(Path.Combine(inputFolder, "Original.inp"))
writer.Write(fileContents)
writer.Close()
End Using
'Now read the simulation executable. The same namespace issues noted above still apply.
'Since this is a binary file we use a file stream to read into a byte buffer
Dim buffer() As Byte = Nothing
Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Simulation.exe")
If fileStream IsNot Nothing Then
ReDim buffer(fileStream.Length)
fileStream.Read(buffer, 0, fileStream.Length)
fileStream.Close()
End If
End Using
'Now write the byte buffer with the contents of the executable file to the root folder
If buffer IsNot Nothing Then
Using exeStream As New FileStream(Path.Combine(appFolder, "Simulation.exe"), FileMode.Create, FileAccess.Write, FileShare.None)
exeStream.Write(buffer, 0, buffer.Length)
exeStream.Close()
End Using
End If
End Sub
End Module
You will also have to add logic to determine if the files have been extracted so it doesn't happen every time the GUI is invoked. That's a big reason why an installation program might be the correct answer.

How do I attach PDF files in VB.NET?

Assume that I have a Windows Forms app in which I type all the details of an employee and I need to attach his resume (which is in a PDF format) to his details. When I click the save button, all his details which he has entered can be stored into a table.
How do I attach a PDF file into the app which the user can do by clicking a button after typing all his details?
When the user clicks the save button, how do I save the file that has been attached by the user?
When I retrieve the employee details, I want the file which was attached also to be displayed and shown.
Use the OpenFileDialog to allow the user to browse to the PDF they want to attach. Store the file path of the selected PDF.
When the user saves the record, read the file the user selected into a byte[], and save that byte[] into a database field (probably a blob field). (See here)
To read it back in, do the reverse. Get the byte[] out of the database, and reassemble it into a file, save the file as a temp PDF and then start a process to load it. (See here)
Here's a simple example, with no error checking, demonstrating Michael Shimmins' good suggestion.
Dim ofd As New OpenFileDialog()
'Set ofd settings'
If ofd.ShowDialog() = DialogResult.Cancel Then
Return
End If
Dim pdfData As Byte() = File.ReadAllBytes(ofd.FileName)
'Save it to your database.'
However, I suggest you consider copying the files to a file server instead of dealing with the binary data and BLOBs if you can help it. BLOBs have been known to kill database performance on some platforms. Here's one reference. Google "blob database performance" for more.
Here's an example of copying the file to a file server.
Dim newFileName As String = UniqueFileNameGeneratingFunction()
File.Copy(odf.FileName, newFileName)
'Save newFileName to your database as a string, and retrieve the file as needed'
'from the file server'