因为工作需要写了一个文件传输的东东
使用了System.Runtime.Remoting
客户端接口类:
Namespace Nail.Net.Remoting.Trans
Public Interface iTransFile
Function SendFile(ByVal FileName As String, ByVal bytes() As Byte, ByVal ClientCrc As Integer) As Integer
Function CompareFile(ByVal FileName As String, ByVal ClientCrc As Integer) As Boolean
Function GetFileDet(ByVal FileName As String) As Integer
Function GetFileInfo(ByVal FileName As String) As Byte()
Function GetFileInfo(ByVal FileName As String, ByVal Length As Integer) As Byte()
Function GetFileInfo(ByVal FileName As String, ByVal Point As Integer, ByVal Length As Integer) As Byte()
Property ServerTransSaveFilePath() As String
Property ServerTempFilePath() As String
End Interface
End Namespace
客户端类:(文件传输的实现在此)(包含1个窗体 后面窗体代码我会贴出来)(请添加引用System.Runtime.Remoting .net默认是不引用的 服务器端类同样)
Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.IO
Imports System.Threading
Namespace Nail.Net.Remoting.Client
Public Class FileTransClient
Private TransTempSize As Integer
Private TransType As TrType
Private TransPort As Integer
Private TransServerClassName As String
Private TransServerIpAdr As String
Private TransCRC32 As Integer
Private Tsc As Nail.Net.Remoting.Trans.iTransFile
Private Senconds As Integer = 10
Private WithEvents Timer1 As New System.Windows.Forms.Timer
Private NewTransForm As FrmTransfersUI
Private TransFileName As String
'文件全路径
Public Property FullFileName() As String
Get
Return TransFileName
End Get
Set(ByVal Value As String)
TransFileName = Value
End Set
End Property
'缓存属性
Public Property TempSize() As Integer
Get
Return TempSize
End Get
Set(ByVal Value As Integer)
Me.TransTempSize = Value
End Set
End Property
'端口属性
Public Property Port() As Integer
Get
Return Port
End Get
Set(ByVal Value As Integer)
Me.TransPort = Value
End Set
End Property
'服务器端服务名属性
Public Property ServerClassName() As String
Get
Return ServerClassName
End Get
Set(ByVal Value As String)
Me.TransServerClassName = Value
End Set
End Property
'服务器端IP地址属性
Public Property ServerIpAdr() As String
Get
Return ServerIpAdr()
End Get
Set(ByVal Value As String)
Me.TransServerIpAdr = Value
End Set
End Property
'枚举传输类型
Enum TrType
TCP = 1
HTTP = 2
End Enum
'传输类别属性
Public Property Type() As TrType
Get
Return TransType
End Get
Set(ByVal Value As TrType)
Me.TransType = Value
End Set
End Property
'连接服务器
Public Sub ConnetServer()
Try
Select Case TransType
Case TrType.TCP
Tsc = Activator.GetObject(GetType(Nail.Net.Remoting.Trans.iTransFile), "tcp://" & TransServerIpAdr & ":" & TransPort & "/" & TransServerClassName)
Case TrType.HTTP
Tsc = Activator.GetObject(GetType(Nail.Net.Remoting.Trans.iTransFile), "http://" & TransServerIpAdr & ":" & TransPort & "/" & TransServerClassName)
End Select
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Function CheckFileInfo(ByVal FileName As String) As Integer
Dim crc As New Nail.FileTools.clsCrc32
Return crc.CalcuateFile(FileName)
End Function
Public Sub SendFile()
ShowUI()
Timer1.Interval = 10
Timer1.Start()
Dim SendThead As New Thread(AddressOf SendFileToServer)
SendThead.Start()
End Sub
'发送文件方法
Private Sub SendFileToServer()
Dim Crc As Integer = CheckFileInfo(TransFileName)
Dim Fif As New FileInfo(TransFileName)
Dim FileSm As New FileStream(TransFileName, FileMode.Open, FileAccess.Read, FileShare.Read)
If FileSm.Length = 0 Then
MsgBox("这是个空文件 请选择一个有效文件!")
Exit Sub
End If
Dim bty(TransTempSize - 1) As Byte
Dim Star As Integer = 0
Dim i As Long
NewTransForm.MaxOfProgress = FileSm.Length '进度条总量
NewTransForm.FileSize = Fif.Length / 1000 & "Kbyte" '文件大小
NewTransForm.TransfersFileName = Fif.Name '文件名
Try
If (Fif.Length / TransTempSize).ToString.IndexOf(".") > 0 Then
If (Fif.Length / TransTempSize).ToString.Substring(0, 1) = "0" Then
ReDim bty(Fif.Length - 1)
FileSm.Read(bty, 0, bty.Length)
Tsc.SendFile(Fif.Name, bty, Crc)
With NewTransForm
.RateOfProgress = bty.Length '进度条当前值
.PercentOfProgress = "100%" '已完成的百分比
.LeaveTime = "完成" '剩余时间
.Transed = bty.Length & "byte" '已传输文件量
.RateOfTransfers = Math.Floor(bty.Length / Senconds) & "Kbyte/秒" '传输速度
End With
Else
For i = Math.Floor(Fif.Length / TransTempSize) To 1 Step -1
FileSm.Read(bty, 0, bty.Length)
Tsc.SendFile(Fif.Name, bty, Crc)
With NewTransForm
.RateOfProgress = Star
.PercentOfProgress = Math.Floor(NewTransForm.pgbTransfers.Value / NewTransForm.pgbTransfers.Maximum * 100) & "%"
If Star > 0 Then
.LeaveTime = TimeSpan.FromSeconds(Math.Floor((Fif.Length - Star) / 1000 / Math.Floor(Star / Senconds))).ToString
End If
.Transed = Star / 1000 & "Kbyte"
.RateOfTransfers = Math.Floor(Star / Senconds) & "Kbyte/秒"
End With
Star += TransTempSize
FileSm.Seek(Star, SeekOrigin.Begin)
Next
ReDim bty((Fif.Length - (Math.Floor(Fif.Length / TransTempSize)) * TransTempSize) - 1)
FileSm.Read(bty, 0, bty.Length)
Tsc.SendFile(Fif.Name, bty, Crc)
With NewTransForm
.RateOfProgress = .pgbTransfers.Maximum '进度条当前值
.PercentOfProgress = "100%"
.LeaveTime = "完成"
.Transed = FileSm.Length / 1000 & "Kbyte"
.RateOfTransfers = Math.Floor(Star / Senconds) & "Kbyte/秒" '传输速度
End With
End If
Else
For i = Math.Floor(Fif.Length / TransTempSize) To 1 Step -1
FileSm.Read(bty, 0, bty.Length)
Tsc.SendFile(Fif.Name, bty, Crc)
With NewTransForm
.RateOfProgress = Star
.PercentOfProgress = Math.Floor(NewTransForm.pgbTransfers.Value / NewTransForm.pgbTransfers.Maximum * 100) & "%"
.Transed = Star / 1000 & "Kbyte"
If Star > 0 Then
.LeaveTime = TimeSpan.FromSeconds(Math.Floor((Fif.Length - Star) / 1000 / Math.Floor(Star / Senconds))).ToString
End If
.RateOfTransfers = Math.Floor(Star / Senconds) & "Kbyte/秒" '传输速度
End With
Star += TransTempSize
FileSm.Seek(Star, SeekOrigin.Begin)
Next
NewTransForm.LeaveTime = "完成"
End If
NewTransForm.btnCancle.Text = "确 定"
Me.Timer1.Stop()
Senconds = 10
'NewTransForm.Close()
'MsgBox("传输完成")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
FileSm.Close()
End Sub
'接受文件方法
Public Function GetFile(ByVal _filename As String) As Integer
End Function
'记时
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Senconds += 10
End Sub
Public Function ShowUI() As Integer
NewTransForm = New FrmTransfersUI
NewTransForm.Show()
Return 0
End Function
End Class
End Namespace
客户端窗体(显示文件传输的进度 时间 大小 。。。。。)
Public Class FrmTransfersUI
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents pgbTransfers As System.Windows.Forms.ProgressBar
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents lblRateOfTransfers As System.Windows.Forms.Label
Friend WithEvents lblTransfersFileName As System.Windows.Forms.Label
Friend WithEvents btnCancle As System.Windows.Forms.Button
Friend WithEvents lblFileSize As System.Windows.Forms.Label
Friend WithEvents lblLeaveTime As System.Windows.Forms.Label
Friend WithEvents lblPercentOfProgress As System.Windows.Forms.Label
Friend WithEvents lblTransed As System.Windows.Forms.Label
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Me.Button1 = New System.Windows.Forms.Button
3Me.pgbTransfers = New System.Windows.Forms.ProgressBar
4Me.Button2 = New System.Windows.Forms.Button
5Me.lblRateOfTransfers = New System.Windows.Forms.Label
6Me.lblTransed = New System.Windows.Forms.Label
7Me.lblTransfersFileName = New System.Windows.Forms.Label
8Me.btnCancle = New System.Windows.Forms.Button
9Me.lblFileSize = New System.Windows.Forms.Label
10Me.lblLeaveTime = New System.Windows.Forms.Label
11Me.lblPercentOfProgress = New System.Windows.Forms.Label
12Me.SuspendLayout()
13'
14'Button1
15'
16Me.Button1.Dock = System.Windows.Forms.DockStyle.Fill
17Me.Button1.Enabled = False
18Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System
19Me.Button1.Location = New System.Drawing.Point(0, 0)
20Me.Button1.Name = "Button1"
21Me.Button1.Size = New System.Drawing.Size(448, 136)
22Me.Button1.TabIndex = 0
23'
24'pgbTransfers
25'
26Me.pgbTransfers.Location = New System.Drawing.Point(10, 16)
27Me.pgbTransfers.Name = "pgbTransfers"
28Me.pgbTransfers.Size = New System.Drawing.Size(430, 12)
29Me.pgbTransfers.TabIndex = 1
30'
31'Button2
32'
33Me.Button2.Enabled = False
34Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.System
35Me.Button2.Location = New System.Drawing.Point(4, 4)
36Me.Button2.Name = "Button2"
37Me.Button2.Size = New System.Drawing.Size(442, 130)
38Me.Button2.TabIndex = 2
39'
40'lblRateOfTransfers
41'
42Me.lblRateOfTransfers.FlatStyle = System.Windows.Forms.FlatStyle.System
43Me.lblRateOfTransfers.Location = New System.Drawing.Point(10, 36)
44Me.lblRateOfTransfers.Name = "lblRateOfTransfers"
45Me.lblRateOfTransfers.Size = New System.Drawing.Size(220, 14)
46Me.lblRateOfTransfers.TabIndex = 3
47Me.lblRateOfTransfers.Text = "传输速度:"
48'
49'lblTransed
50'
51Me.lblTransed.FlatStyle = System.Windows.Forms.FlatStyle.System
52Me.lblTransed.Location = New System.Drawing.Point(10, 58)
53Me.lblTransed.Name = "lblTransed"
54Me.lblTransed.Size = New System.Drawing.Size(220, 14)
55Me.lblTransed.TabIndex = 4
56Me.lblTransed.Text = "已 传 送:"
57'
58'lblTransfersFileName
59'
60Me.lblTransfersFileName.FlatStyle = System.Windows.Forms.FlatStyle.System
61Me.lblTransfersFileName.Location = New System.Drawing.Point(10, 80)
62Me.lblTransfersFileName.Name = "lblTransfersFileName"
63Me.lblTransfersFileName.Size = New System.Drawing.Size(220, 14)
64Me.lblTransfersFileName.TabIndex = 5
65Me.lblTransfersFileName.Text = "传输文件:"
66'
67'btnCancle
68'
69Me.btnCancle.FlatStyle = System.Windows.Forms.FlatStyle.System
70Me.btnCancle.Location = New System.Drawing.Point(192, 104)
71Me.btnCancle.Name = "btnCancle"
72Me.btnCancle.TabIndex = 6
73Me.btnCancle.Text = "取 消"
74'
75'lblFileSize
76'
77Me.lblFileSize.FlatStyle = System.Windows.Forms.FlatStyle.System
78Me.lblFileSize.Location = New System.Drawing.Point(246, 80)
79Me.lblFileSize.Name = "lblFileSize"
80Me.lblFileSize.Size = New System.Drawing.Size(192, 14)
81Me.lblFileSize.TabIndex = 9
82Me.lblFileSize.Text = "文件大小:"
83'
84'lblLeaveTime
85'
86Me.lblLeaveTime.FlatStyle = System.Windows.Forms.FlatStyle.System
87Me.lblLeaveTime.Location = New System.Drawing.Point(246, 58)
88Me.lblLeaveTime.Name = "lblLeaveTime"
89Me.lblLeaveTime.Size = New System.Drawing.Size(192, 14)
90Me.lblLeaveTime.TabIndex = 8
91Me.lblLeaveTime.Text = "剩余时间:"
92'
93'lblPercentOfProgress
94'
95Me.lblPercentOfProgress.FlatStyle = System.Windows.Forms.FlatStyle.System
96Me.lblPercentOfProgress.Location = New System.Drawing.Point(246, 36)
97Me.lblPercentOfProgress.Name = "lblPercentOfProgress"
98Me.lblPercentOfProgress.Size = New System.Drawing.Size(192, 14)
99Me.lblPercentOfProgress.TabIndex = 7
100Me.lblPercentOfProgress.Text = "传输进度:"
101'
102'FrmTransfersUI
103'
104Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
105Me.ClientSize = New System.Drawing.Size(448, 136)
106Me.Controls.Add(Me.lblFileSize)
107Me.Controls.Add(Me.lblLeaveTime)
108Me.Controls.Add(Me.lblPercentOfProgress)
109Me.Controls.Add(Me.btnCancle)
110Me.Controls.Add(Me.lblTransfersFileName)
111Me.Controls.Add(Me.lblTransed)
112Me.Controls.Add(Me.lblRateOfTransfers)
113Me.Controls.Add(Me.pgbTransfers)
114Me.Controls.Add(Me.Button2)
115Me.Controls.Add(Me.Button1)
116Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
117Me.Name = "FrmTransfersUI"
118Me.ShowInTaskbar = False
119Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
120Me.Text = "FrmTransfersUI"
121Me.TopMost = True
122Me.ResumeLayout(False)
123
124End Sub
125
126#End Region
127
128Public Event UserCancle()
129
130Public WriteOnly Property MaxOfProgress()
131Set(ByVal Value)
132Me.pgbTransfers.Maximum = Value
133End Set
134End Property
135
136Public WriteOnly Property RateOfProgress()
137Set(ByVal Value)
138Me.pgbTransfers.Value = Value
139End Set
140End Property
141
142Public WriteOnly Property RateOfTransfers() As String
143Set(ByVal Value As String)
144Me.lblRateOfTransfers.Text = "传输速度:" & Value
145End Set
146End Property
147
148Public WriteOnly Property Transed() As String
149Set(ByVal Value As String)
150Me.lblTransed.Text = "已 传 送:" & Value
151End Set
152End Property
153
154Public WriteOnly Property TransfersFileName() As String
155Set(ByVal Value As String)
156Me.lblTransfersFileName.Text = "传输文件:" & Value
157End Set
158End Property
159
160Public WriteOnly Property PercentOfProgress() As String
161Set(ByVal Value As String)
162Me.lblPercentOfProgress.Text = "传输进度:" & Value
163End Set
164End Property
165
166Public WriteOnly Property LeaveTime() As String
167Set(ByVal Value As String)
168Me.lblLeaveTime.Text = "剩余时间:" & Value
169End Set
170End Property
171
172Public WriteOnly Property FileSize() As String
173Set(ByVal Value As String)
174Me.lblFileSize.Text = "文件大小:" & Value
175End Set
176End Property
177
178
179Private Sub btnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancle.Click
180
181If Me.btnCancle.Text = "确 定" Then
182Me.Close()
183Else
184RaiseEvent UserCancle()
185End If
186
187
188End Sub
189End Class
190
191
192服务器端类 下面还有个模块
193
194Imports System.Runtime
195Imports System.Runtime.Remoting
196Imports System.Runtime.Remoting.Channels
197Imports System.Runtime.Remoting.Channels.Tcp
198Imports System.IO
199
200Namespace Nail.Net.Remoting.Server
201Public Class FileTransServer
202
203Private TransServerClassName As String
204Private TransType As TrType
205Private TransPort As Integer
206Private TransCRC32 As Integer
207
208Enum TrType
209TCP = 1
210HTTP = 2
211End Enum
212
213Public Property Type() As TrType
214Get
215Return TransType
216End Get
217Set(ByVal Value As TrType)
218Me.TransType = Value
219End Set
220End Property
221
222Public Property ServerTransSaveFilePath() As String
223Get
224Return TransSaveFilePath
225End Get
226Set(ByVal Value As String)
227TransSaveFilePath = Value
228End Set
229End Property
230
231Public Property ServerTempFilePath() As String
232Get
233Return TempFilePath
234End Get
235Set(ByVal Value As String)
236TempFilePath = Value
237End Set
238End Property
239
240Public Property ServerClassName() As String
241Get
242Return ServerClassName
243End Get
244Set(ByVal Value As String)
245Me.TransServerClassName = Value
246End Set
247End Property
248
249Public Property Port() As Integer
250Get
251Return Port
252End Get
253Set(ByVal Value As Integer)
254Me.TransPort = Value
255End Set
256End Property
257
258Public Sub StarServer()
259Select Case TransType
260Case TrType.TCP
261Dim a As New System.Runtime.Remoting.Channels.Tcp.TcpChannel(TransPort)
262ChannelServices.RegisterChannel(a)
263Case TrType.HTTP
264Dim a As New System.Runtime.Remoting.Channels.Http.HttpChannel(TransPort)
265ChannelServices.RegisterChannel(a)
266End Select
267System.Runtime.Remoting.RemotingConfiguration.ApplicationName = TransServerClassName
268RemotingConfiguration.RegisterWellKnownServiceType(GetType(TransFile), TransServerClassName, WellKnownObjectMode.SingleCall)
269
270End Sub
271
272End Class
273
274Public Class TransFile
275Inherits MarshalByRefObject
276Implements Nail.Net.Remoting.Trans.iTransFile
277
278Public Function CompareFile(ByVal FileName As String, ByVal ClientCrc As Integer) As Boolean Implements Nail.Net.Remoting.Trans.iTransFile.CompareFile
279Dim crc As New Nail.FileTools.clsCrc32
280If crc.CalcuateFile(FileName) = ClientCrc Then
281Return True
282Else
283Return False
284End If
285End Function
286
287Public Function GetFileDet(ByVal FileName As String) As Integer Implements Nail.Net.Remoting.Trans.iTransFile.GetFileDet
288Dim c As New FileInfo(FileName)
289Return c.Length
290End Function
291
292Public Function GetFileInfo(ByVal FileName As String) As Byte() Implements Nail.Net.Remoting.Trans.iTransFile.GetFileInfo
293Dim FullName As String = FileName
294Dim c As New FileInfo(FullName)
295Return GetFileInfo(FileName, 0, c.Length)
296End Function
297
298Public Function GetFileInfo(ByVal FileName As String, ByVal Length As Integer) As Byte() Implements Nail.Net.Remoting.Trans.iTransFile.GetFileInfo
299Return GetFileInfo(FileName, 0, Length)
300End Function
301
302Public Function GetFileInfo(ByVal FileName As String, ByVal Point As Integer, ByVal Length As Integer) As Byte() Implements Nail.Net.Remoting.Trans.iTransFile.GetFileInfo
303Dim FullName As String = FileName
304Dim c As New FileInfo(FullName)
305Dim x As New FileStream(FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
306Dim bty(Length - 1) As Byte
307x.Seek(Point, SeekOrigin.Begin)
308x.Read(bty, 0, Length)
309Return bty
310End Function
311
312Public Function SendFile(ByVal FileName As String, ByVal bytes() As Byte, ByVal ClientCrc As Integer) As Integer Implements Nail.Net.Remoting.Trans.iTransFile.SendFile
313Try
314Dim a As New FileStream(TempFilePath & FileName, FileMode.OpenOrCreate)
315a.Seek(a.Length, SeekOrigin.Begin)
316a.Write(bytes, 0, bytes.Length)
317a.Close()
318Return 0
319Catch ex As Exception
320MsgBox(ex.ToString)
321Return 1
322Finally
323'Dim Finf As New FileInfo(TempFilePath & FileName)
324'If CompareFile(TempFilePath & FileName, ClientCrc) = True Then
325' '拷贝临时文件()
326' File.Copy(TempFilePath & FileName, TransSaveFilePath & FileName, True)
327'Else
328' '删除临时文件()
329' Finf.Delete()
330'End If
331End Try
332
333End Function
334
335Public Property ServerTempFilePath() As String Implements Nail.Net.Remoting.Trans.iTransFile.ServerTempFilePath
336Get
337Return TempFilePath
338End Get
339Set(ByVal Value As String)
340TempFilePath = Value
341End Set
342End Property
343
344Public Property ServerTransSaveFilePath() As String Implements Nail.Net.Remoting.Trans.iTransFile.ServerTransSaveFilePath
345Get
346Return TransSaveFilePath
347End Get
348Set(ByVal Value As String)
349TransSaveFilePath = Value
350End Set
351End Property
352End Class
353
354End Namespace
355
356模块:
357
358Module Server
359Public TransSaveFilePath As String
360Public TempFilePath As String
361End Module
362
363使用方法:
364
365服务器端
366
367示例:
368
369Dim Server As New Nail.Net.Remoting.Server.FileTransServer
370Server.ServerClassName = "aaa"
371Server.Port = 10001
372Server.Type = Nail.Net.Remoting.Server.FileTransServer.TrType.HTTP
373Server.ServerTempFilePath = "f:\"
374Server.ServerTransSaveFilePath = "e:\"
375Server.StarServer()
376
377客户端
378
379cl.Port = 10001
380cl.ServerClassName = "aaa"
381cl.TempSize = 102400
382cl.ServerIpAdr = "localhost"
383cl.FullFileName = "c:\111.rar"
384cl.Type = Nail.Net.Remoting.Client.FileTransClient.TrType.HTTP
385cl.ConnetServer()
386
387发送文件:
388
389cl.SendFile()
390
391最后忘记贴文件校对了 照书上搞的CRC32
392
393Imports System.IO
394
395Namespace Nail.FileTools
396
397Public Class clsCrc32
398Private Const TABLESIZE As Integer = 256
399Private Const DEFAULTPOLYNOMIAL As Integer = &HEDB88320
400Private Const DEFAULTIALVALUE As Integer = &HFFFFFFFF
401Private lookup(TABLESIZE - 1) As Integer
402Private crcPolynomial As Integer = 0
403
404Public Sub New()
405Me.New(DEFAULTPOLYNOMIAL)
406End Sub
407
408Public Sub New(ByVal crcPolynomial As Integer)
409Me.crcPolynomial = crcPolynomial
410InitLookupTable()
411End Sub
412
413Public Property Polynomial() As Integer
414Get
415Return crcPolynomial
416End Get
417Set(ByVal Value As Integer)
418Me.crcPolynomial = Value
419InitLookupTable()
420End Set
421End Property
422
423Public Overloads Function CalculateBlock(ByVal bytes() As Byte) As Integer
424Return CalculateBlock(bytes, 0, bytes.Length)
425End Function
426
427Public Overloads Function CalculateBlock(ByVal bytes() As Byte, ByVal index As Integer, ByVal length As Integer) As Integer
428Return CalculateBlock(bytes, index, length, DEFAULTIALVALUE)
429End Function
430
431Public Overloads Function CalculateBlock(ByVal bytes() As Byte, ByVal index As Integer, ByVal length As Integer, ByVal initialValue As Integer) As Integer
432If bytes Is Nothing Then
433
434ElseIf index < 0 Or length <= 0 Or index + length > bytes.Length Then
435
436End If
437Return Not internalcalculateBlock(bytes, index, length, initialValue)
438End Function
439
440Private Function internalcalculateBlock(ByVal bytes() As Byte, ByVal index As Integer, ByVal length As Integer, ByVal initialValue As Integer) As Integer
441Dim crc As Integer = initialValue
442Dim shiftCrc As Integer
443
444Dim position As Integer
445For position = index To length - 1
446shiftCrc = crc And &HFFFFFF00
447shiftCrc = shiftCrc / &H100
448shiftCrc = shiftCrc And &HFFFFFF
449crc = shiftCrc Xor lookup(bytes(position)) Xor (crc And &HFF)
450Next
451Return crc
452End Function
453
454Public Overloads Function CalcuateFile(ByVal path As String) As Integer
455Return CalcuateFile(path, DEFAULTIALVALUE)
456
457End Function
458
459Public Overloads Function CalcuateFile(ByVal path As String, ByVal initialValue As Integer) As Integer
460If path Is Nothing Then
461
462ElseIf path.Length = 0 Then
463
464End If
465
466Return Not InternalCalculateFile(path, initialValue)
467End Function
468
469Private Function InternalCalculateFile(ByVal path As String, ByVal initialValue As Integer) As Integer
470
471Const blockSize As Integer = 4096
472Dim count As Integer
473Dim inStream As System.IO.FileStream
474Dim bytes(blockSize - 1) As Byte
475Dim crc As Integer = initialValue
476Try
477inStream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read)
478While inStream.Position < inStream.Length
479count = inStream.Read(bytes, 0, blockSize)
480crc = internalcalculateBlock(bytes, 0, count, crc)
481End While
482Finally
483If Not inStream Is Nothing Then
484inStream.Close()
485End If
486End Try
487Return crc
488End Function
489
490Private Sub InitLookupTable()
491Dim bytecount, bitcount As Integer
492Dim crc, shiftCrc As Integer
493For bytecount</system.diagnostics.debuggerstepthrough()>