當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET NotSupportedException類代碼示例

本文整理匯總了VB.NET中System.NotSupportedException的典型用法代碼示例。如果您正苦於以下問題:VB.NET NotSupportedException類的具體用法?VB.NET NotSupportedException怎麽用?VB.NET NotSupportedException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了NotSupportedException類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Main

' 導入命名空間
Imports System.IO
Imports System.Text
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim enc As Encoding = Encoding.Unicode
      Dim value As String = "This is a string to persist."
      Dim bytes() As Byte = enc.GetBytes(value)

      Dim fs As New FileStream(".\TestFile.dat", 
                               FileMode.Open,
                               FileAccess.Read)
      Dim t As Task = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
      Dim t2 As Task = t.ContinueWith(Sub(a) fs.WriteAsync(bytes, 0, bytes.Length)) 
      t2.Wait()
      fs.Close()
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:20,代碼來源:NotSupportedException

輸出:

Unhandled Exception: System.NotSupportedException: Stream does not support writing.
at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
, Boolean serializeAsynchronously)
at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
teObject)
at System.IO.Stream.<>c.b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
hod, Func`3 endMethod)
at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
at Example.Main()

示例2: Main

' 導入命名空間
Imports System.IO
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim name As String = ".\TestFile.dat"
      Dim fs As New FileStream(name, 
                               FileMode.Create,
                               FileAccess.Write)
      Console.WriteLine("Filename: {0}, Encoding: {1}", 
                        name, FileUtilities.GetEncodingType(fs))
   End Sub
End Module

Public Class FileUtilities
   Public Enum EncodingType As Integer
      None = 0
      Unknown = -1
      Utf8 = 1
      Utf16 = 2
      Utf32 = 3
   End Enum
   
   Public Shared Function GetEncodingType(fs As FileStream) As EncodingType
      Dim bytes(3) As Byte
      Dim t As Task(Of Integer) = fs.ReadAsync(bytes, 0, 4)
      t.Wait()
      Dim bytesRead As Integer = t.Result
      If bytesRead < 2 Then Return EncodingType.None
      
      If bytesRead >= 3 And (bytes(0) = &hEF AndAlso bytes(1) = &hBB AndAlso bytes(2) = &hBF) Then
         Return EncodingType.Utf8
      End If
      
      If bytesRead = 4 Then 
         Dim value As UInteger = BitConverter.ToUInt32(bytes, 0)
         If value = &h0000FEFF Or value = &hFEFF0000 Then
            Return EncodingType.Utf32
         End If
      End If
      
      Dim value16 As UInt16 = BitConverter.ToUInt16(bytes, 0)
      If value16 = &hFEFF Or value16 = &hFFFE Then 
         Return EncodingType.Utf16
      End If
      
      Return EncodingType.Unknown
   End Function
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:50,代碼來源:NotSupportedException

輸出:

Unhandled Exception: System.NotSupportedException: Stream does not support reading.
at System.IO.Stream.BeginReadInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state,
Boolean serializeAsynchronously)
at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object stat
eObject)
at System.IO.Stream.<>c.b__43_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, O
bject state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
hod, Func`3 endMethod)
at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
at FileUtilities.GetEncodingType(FileStream fs)
at Example.Main()

示例3: GetEncodingType

Public Class FileUtilities
   Public Enum EncodingType As Integer
      None = 0
      Unknown = -1
      Utf8 = 1
      Utf16 = 2
      Utf32 = 3
   End Enum
   
   Public Shared Function GetEncodingType(fs As FileStream) As EncodingType
      If Not fs.CanRead Then
         Return EncodingType.Unknown

      Dim bytes(3) As Byte
      Dim t As Task(Of Integer) = fs.ReadAsync(bytes, 0, 4)
      t.Wait()
      Dim bytesRead As Integer = t.Result
      If bytesRead < 2 Then Return EncodingType.None
      
      If bytesRead >= 3 And (bytes(0) = &hEF AndAlso bytes(1) = &hBB AndAlso bytes(2) = &hBF) Then
         Return EncodingType.Utf8
      End If
      
      If bytesRead = 4 Then 
         Dim value As UInteger = BitConverter.ToUInt32(bytes, 0)
         If value = &h0000FEFF Or value = &hFEFF0000 Then
            Return EncodingType.Utf32
         End If
      End If
      
      Dim value16 As UInt16 = BitConverter.ToUInt16(bytes, 0)
      If value16 = &hFEFF Or value16 = &hFFFE Then 
         Return EncodingType.Utf16
      End If
      
      Return EncodingType.Unknown
   End Function
End Class
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:38,代碼來源:NotSupportedException

輸出:

Filename: .\TestFile.dat, Encoding: Unknown


注:本文中的System.NotSupportedException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。