Vb.net 警告变量';csCryptoStream';在指定值之前使用。运行时可能会导致空引用异常
我是VB.Net的新手。我正试图调试我的项目,但我得到一个警告:变量“csCryptoStream”在赋值之前就已经被使用了。运行时可能会导致空引用异常 我怎样才能修好它Vb.net 警告变量';csCryptoStream';在指定值之前使用。运行时可能会导致空引用异常,vb.net,encryption,filestream,Vb.net,Encryption,Filestream,我是VB.Net的新手。我正试图调试我的项目,但我得到一个警告:变量“csCryptoStream”在赋值之前就已经被使用了。运行时可能会导致空引用异常 我怎样才能修好它 Private Sub EncryptOrDecryptFile(strInputFile As String, strOutputFile As String, bytKey As Byte(), bytIV As Byte(), Direction As crypt.CryptoAction) ' The foll
Private Sub EncryptOrDecryptFile(strInputFile As String, strOutputFile As String, bytKey As Byte(), bytIV As Byte(), Direction As crypt.CryptoAction)
' The following expression was wrapped in a checked-statement
Try
Me.fsInput = New FileStream(strInputFile, FileMode.Open, FileAccess.Read)
Me.fsOutput = New FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
Me.fsOutput.SetLength(0L)
Dim bytBuffer As Byte() = New Byte(4096) {}
Dim lngBytesProcessed As Long = 0L
Dim lngFileLength As Long = Me.fsInput.Length
Dim cspRijndael As RijndaelManaged = New RijndaelManaged()
Me.pbStatus.Value = 0
Me.pbStatus.Maximum = 100
Dim csCryptoStream As CryptoStream
Select Case Direction
Case crypt.CryptoAction.ActionEncrypt
csCryptoStream = New CryptoStream(Me.fsOutput, cspRijndael.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write)
Case crypt.CryptoAction.ActionDecrypt
csCryptoStream = New CryptoStream(Me.fsOutput, cspRijndael.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write)
End Select
While lngBytesProcessed < lngFileLength
Dim intBytesInCurrentBlock As Integer = Me.fsInput.Read(bytBuffer, 0, 4096)
Warning ---> csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
' The following expression was wrapped in a unchecked-expression
lngBytesProcessed += CLng(intBytesInCurrentBlock)
' The following expression was wrapped in a unchecked-expression
Me.pbStatus.Value = CInt(Math.Round(CDec(lngBytesProcessed) / CDec(lngFileLength) * 100.0))
End While
Private Sub-encryptorsecryptfile(strInputFile作为字符串,strOutputFile作为字符串,bytKey作为Byte(),bytIV作为Byte(),Direction作为crypt.cryptoction)
'以下表达式被包装在checked语句中
尝试
Me.fsInput=newfilestream(strInputFile,FileMode.Open,FileAccess.Read)
Me.fsOutput=新文件流(strOutputFile、FileMode.OpenOrCreate、FileAccess.Write)
Me.fsOutput.SetLength(0L)
Dim bytBuffer As Byte()=新字节(4096){}
当长度=0L时,处理的字节数变小
Dim lngFileLength As Long=Me.fsInput.Length
Dim cspRijndael As RijndaelManaged=New RijndaelManaged()
Me.pbStatus.Value=0
Me.pbStatus.max=100
Dim csCryptoStream作为加密流
选择案例方向
Case crypt.CryptoAction.ActionEncrypt
csCryptoStream=新加密流(Me.fsOutput,cspRijndael.CreateEncryptor(bytKey,bytIV),CryptoStreamMode.Write)
Case crypt.CryptoAction.ActionDecrypt
csCryptoStream=新加密流(Me.fsOutput,cspRijndael.CreateDecryptor(bytKey,bytIV),CryptoStreamMode.Write)
结束选择
而LngBytes进程小于lngFileLength
Dim INTBYTESINRENTBLOCK作为整数=Me.fsInput.Read(bytBuffer,0,4096)
警告--->csCryptoStream.Write(bytBuffer,0,intBytesInCurrentBlock)
'以下表达式被包装在未选中的表达式中
lngBytesProcessed+=CLng(intBytesInCurrentBlock)
'以下表达式被包装在未选中的表达式中
Me.pbStatus.Value=CInt(数学四舍五入(CDec(LNGBytes处理)/CDec(lngFileLength)*100.0))
结束时
加密流
,如下所示:
Dim csCryptoStream As CryptoStream = Nothing
Case/Select中添加“Case-Else
”子句
If Not IsNothing(csCryptoStream)
' ....
End If
在使用它之前为它赋值可能是首先尝试的显而易见的事情…我在尝试调试时遇到了其他错误:Microsoft.VisualBasic.ApplicationServices.NoStartupFormException类型的未处理异常在Microsoft.VisualBasic中发生。dll@MuhammadFathan如果您在该行中遇到错误,请尝试将其声明为“新建”i、 e.Dim CcryptoStream作为新的加密流