Private Function GetFtpServerCopy(ByVal ftpServer As String, ByVal username As String, _
ByVal password As String, ByVal filepath As String) As Date
Dim ftpFile As String
If Not ftpServer.EndsWith("/") Then
ftpServer &= "/"
End If
ftpFile = filepath + "/Logs/scanReport.csv"
Dim dateStr As String = String.Empty
Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpFile)
Dim creds As New System.Net.NetworkCredential(username, password)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Dim response As System.Net.FtpWebResponse = request.GetResponse
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream("D:\copy.csv", IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Function
Monday, January 31, 2011
Get FTP Server Time
http://social.msdn.microsoft.com/Forums/en/vblanguage/thread/56137d13-9bf5-4f35-9358-88b5009709fa
Private Function GetFtpServerDateTime(ByVal ftpServer As String, ByVal username As String, _
ByVal password As String) As Date
Dim ftpFile As String
If Not ftpServer.EndsWith("/") Then
ftpServer &= "/"
End If
ftpFile = String.Concat(ftpServer, "tmptimecheck.zero")
Dim dateStr As String = String.Empty
Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpFile)
Dim creds As New System.Net.NetworkCredential(username, password)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim response As System.Net.FtpWebResponse = request.GetResponse
If response.StatusCode = Net.FtpStatusCode.ClosingData Then
response.Close()
request = System.Net.FtpWebRequest.Create(ftpFile)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.GetDateTimestamp
response = request.GetResponse
If response.StatusCode = Net.FtpStatusCode.FileStatus Then
dateStr = response.StatusDescription
response.Close()
request = System.Net.FtpWebRequest.Create(ftpFile)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.DeleteFile
response = request.GetResponse
End If
response.Close()
End If
response.Close()
If dateStr.Length = 20 Then
dateStr = dateStr.Substring(4)
Dim year As Integer = CInt(dateStr.Substring(0, 4))
Dim month As Integer = CInt(dateStr.Substring(4, 2))
Dim day As Integer = CInt(dateStr.Substring(6, 2))
Dim hour As Integer = CInt(dateStr.Substring(8, 2))
Dim minute As Integer = CInt(dateStr.Substring(10, 2))
Dim second As Integer = CInt(dateStr.Substring(12, 2))
Return New Date(year, month, day, hour, minute, second)
Else
Throw New Exception("Error while attempting to infer date from server")
End If
End Function
Private Function GetFtpServerDateTime(ByVal ftpServer As String, ByVal username As String, _
ByVal password As String) As Date
Dim ftpFile As String
If Not ftpServer.EndsWith("/") Then
ftpServer &= "/"
End If
ftpFile = String.Concat(ftpServer, "tmptimecheck.zero")
Dim dateStr As String = String.Empty
Dim request As System.Net.FtpWebRequest = System.Net.FtpWebRequest.Create(ftpFile)
Dim creds As New System.Net.NetworkCredential(username, password)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim response As System.Net.FtpWebResponse = request.GetResponse
If response.StatusCode = Net.FtpStatusCode.ClosingData Then
response.Close()
request = System.Net.FtpWebRequest.Create(ftpFile)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.GetDateTimestamp
response = request.GetResponse
If response.StatusCode = Net.FtpStatusCode.FileStatus Then
dateStr = response.StatusDescription
response.Close()
request = System.Net.FtpWebRequest.Create(ftpFile)
request.Credentials = creds
request.Method = System.Net.WebRequestMethods.Ftp.DeleteFile
response = request.GetResponse
End If
response.Close()
End If
response.Close()
If dateStr.Length = 20 Then
dateStr = dateStr.Substring(4)
Dim year As Integer = CInt(dateStr.Substring(0, 4))
Dim month As Integer = CInt(dateStr.Substring(4, 2))
Dim day As Integer = CInt(dateStr.Substring(6, 2))
Dim hour As Integer = CInt(dateStr.Substring(8, 2))
Dim minute As Integer = CInt(dateStr.Substring(10, 2))
Dim second As Integer = CInt(dateStr.Substring(12, 2))
Return New Date(year, month, day, hour, minute, second)
Else
Throw New Exception("Error while attempting to infer date from server")
End If
End Function
Traverse FTP Folder
Dim fwr As FtpWebRequest = FtpWebRequest.Create("ftp://xx.xx.xxx.xxx/")
fwr.Credentials = New NetworkCredential("user", "password")
fwr.Method = WebRequestMethods.Ftp.ListDirectory
Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
str = sr.ReadLine() 'hold the traverse file
End While
sr.Close()
sr = Nothing
fwr = Nothing
fwr.Credentials = New NetworkCredential("user", "password")
fwr.Method = WebRequestMethods.Ftp.ListDirectory
Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
str = sr.ReadLine() 'hold the traverse file
End While
sr.Close()
sr = Nothing
fwr = Nothing
Sunday, January 30, 2011
Get File Size
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo(Application.StartupPath + "\FILE.LOG")
If fileDetail.Length < 1500 Then '1500 bytes
'code here
End If
fileDetail = My.Computer.FileSystem.GetFileInfo(Application.StartupPath + "\FILE.LOG")
If fileDetail.Length < 1500 Then '1500 bytes
'code here
End If
Check for Existing Process Name
While (True)
localByName = Process.GetProcessesByName("notepad")
If localByName.Length = 0 Then
Exit While
End If
End While
localByName = Process.GetProcessesByName("notepad")
If localByName.Length = 0 Then
Exit While
End If
End While
Shell and Wait
ShellandWait("cmd.exe", "/c " + Application.StartupPath + "\Engines\" + temp_Ctr.ToString + "crossparse.bat")
======================================================
Public Sub ShellandWait(ByVal ProcessPath As String, ByVal Param As String)
Dim objProcess As System.Diagnostics.Process
Try
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.FileName = ProcessPath
objProcess.StartInfo.Arguments = Param
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
objProcess.Start()
'Wait until the process passes back an exit code
objProcess.WaitForExit()
'Free resources associated with this process
objProcess.Close()
Catch
MessageBox.Show("Could not start process " & ProcessPath, "Error")
End Try
End Sub
======================================================
Public Sub ShellandWait(ByVal ProcessPath As String, ByVal Param As String)
Dim objProcess As System.Diagnostics.Process
Try
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.FileName = ProcessPath
objProcess.StartInfo.Arguments = Param
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
objProcess.Start()
'Wait until the process passes back an exit code
objProcess.WaitForExit()
'Free resources associated with this process
objProcess.Close()
Catch
MessageBox.Show("Could not start process " & ProcessPath, "Error")
End Try
End Sub
Check for File Lock
Private Function IsFileLocked(ByVal filePath As String) As Boolean
Dim fs As FileStream = Nothing
Dim result As Boolean = False
Try
File.Copy(filePath, POLL_PATH + "\Processing\" + temp(bound) + "~scanReport.csv")
result = False
Catch ex As Exception
'file lock - still copying
result = True
End Try
Return result
End Function
Dim fs As FileStream = Nothing
Dim result As Boolean = False
Try
File.Copy(filePath, POLL_PATH + "\Processing\" + temp(bound) + "~scanReport.csv")
result = False
Catch ex As Exception
'file lock - still copying
result = True
End Try
Return result
End Function
File Watcher
Set Properties:
If e.ChangeType = WatcherChangeTypes.Created Then
While IsFileLocked(process_Folder) = True
System.Threading.Thread.Sleep(1000)
End While
Try
FileSystemWatcher1.Path = POLL_IN
FileSystemWatcher1.NotifyFilter = (NotifyFilters.CreationTime Or _
NotifyFilters.FileName Or _
NotifyFilters.LastAccess Or _
NotifyFilters.LastWrite Or _
NotifyFilters.Size)
FileSystemWatcher1.IncludeSubdirectories = True
FileSystemWatcher1.Filter = "*.*"
FileSystemWatcher1.Path = POLL_IN
FileSystemWatcher1.NotifyFilter = (NotifyFilters.CreationTime Or _
NotifyFilters.FileName Or _
NotifyFilters.LastAccess Or _
NotifyFilters.LastWrite Or _
NotifyFilters.Size)
FileSystemWatcher1.IncludeSubdirectories = True
FileSystemWatcher1.Filter = "*.*"
AddHandler Me.FileSystemWatcher1.Changed, AddressOf MoveFile
AddHandler Me.FileSystemWatcher1.Created, AddressOf MoveFile
AddHandler Me.FileSystemWatcher1.Created, AddressOf MoveFile
FileSystemWatcher1.EnableRaisingEvents = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
Catch ex As Exception
MsgBox(ex.Message)
End Try
========================================================
Adding Handler:
Private Sub MoveFile(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
'for Change Types: Created/Renamed/Deleted/Changed
If e.ChangeType = WatcherChangeTypes.Created Then
While IsFileLocked(process_Folder) = True
System.Threading.Thread.Sleep(1000)
End While
End If
End Sub
End Sub
========================================================
Set Events Handled:
Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created, FileSystemWatcher1.Changed
Try
'code here for file system watch trigger during change and create
'code here for file system watch trigger during change and create
Catch ex As Exception
MsgBox(ex.Message)
End Try
MsgBox(ex.Message)
End Try
End Sub
Write to Text File
Try
File.WriteAllText(POLL_PATH + "\OUT\" + folder + "\832Cross.LOG", "")
Dim fsStream8 As New FileStream(POLL_PATH + "\OUT\" + folder + "\832Cross.LOG", FileMode.Open, FileAccess.ReadWrite)
Dim srWriter As New StreamWriter(fsStream8)
srWriter.WriteLine("write here")
srWriter.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
File.WriteAllText(POLL_PATH + "\OUT\" + folder + "\832Cross.LOG", "")
Dim fsStream8 As New FileStream(POLL_PATH + "\OUT\" + folder + "\832Cross.LOG", FileMode.Open, FileAccess.ReadWrite)
Dim srWriter As New StreamWriter(fsStream8)
srWriter.WriteLine("write here")
srWriter.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Read Text File
Try
If File.Exists(Application.StartupPath + "\config.ini") Then
Dim fsStream7 As New FileStream(Application.StartupPath + "\config.ini", FileMode.Open, FileAccess.Read)
Dim srReader7 As New StreamReader(fsStream7)
Dim readline1 As String
srReader7.BaseStream.Seek(0, SeekOrigin.Begin)
While srReader7.Peek() > -1
readline1 = (srReader7.ReadLine())
End While
srReader7.Close()
Else
MessageBox.Show("Unable to locate " + Application.StartupPath + "\config.ini")
Me.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
If File.Exists(Application.StartupPath + "\config.ini") Then
Dim fsStream7 As New FileStream(Application.StartupPath + "\config.ini", FileMode.Open, FileAccess.Read)
Dim srReader7 As New StreamReader(fsStream7)
Dim readline1 As String
srReader7.BaseStream.Seek(0, SeekOrigin.Begin)
While srReader7.Peek() > -1
readline1 = (srReader7.ReadLine())
End While
srReader7.Close()
Else
MessageBox.Show("Unable to locate " + Application.StartupPath + "\config.ini")
Me.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Imports
Imports System.Diagnostics.Process
Imports System.IO
Imports System.Text
Imports System.Net
Imports System
'Excel
Imports System.Data
Imports System.Windows.Forms
'DB
Imports System.Console
Imports System.Data.SqlClient
'Server-Client
Imports System.Text.RegularExpressions
Imports System.Net.Sockets
Imports System.Threading
Imports Microsoft.Win32
Imports System.IO
Imports System.Text
Imports System.Net
Imports System
'Excel
Imports System.Data
Imports System.Windows.Forms
'DB
Imports System.Console
Imports System.Data.SqlClient
'Server-Client
Imports System.Text.RegularExpressions
Imports System.Net.Sockets
Imports System.Threading
Imports Microsoft.Win32
Add Seconds
Dim date1 As DateTime
date1 = System.DateTime.Now.ToLongTimeString
date1 = date1.AddSeconds(30)
date1 = System.DateTime.Now.ToLongTimeString
date1 = date1.AddSeconds(30)
Sunday, January 23, 2011
C# 1: Forms
Set Properties:
AcceptButton - for Enter key
CancelButton - for ESC key
Exit - this.close();
AcceptButton - for Enter key
CancelButton - for ESC key
Exit - this.close();
Subscribe to:
Comments (Atom)

