本文整理汇总了C#中System.NotSupportedException类的典型用法代码示例。如果您正苦于以下问题:C# NotSupportedException类的具体用法?C# NotSupportedException怎么用?C# NotSupportedException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NotSupportedException类属于System命名空间,在下文中一共展示了NotSupportedException类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
Encoding enc = Encoding.Unicode;
String value = "This is a string to persist.";
Byte[] bytes = enc.GetBytes(value);
FileStream fs = new FileStream(@".\TestFile.dat",
FileMode.Open,
FileAccess.Read);
Task t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length);
Task t2 = t.ContinueWith( (a) => fs.WriteAsync(bytes, 0, bytes.Length) );
await t2;
fs.Close();
}
}
输出:
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
//引入命名空间
using System;
using System.IO;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
String name = @".\TestFile.dat";
var fs = new FileStream(name,
FileMode.Create,
FileAccess.Write);
Console.WriteLine("Filename: {0}, Encoding: {1}",
name, await FileUtilities.GetEncodingType(fs));
}
}
public class FileUtilities
{
public enum EncodingType
{ None = 0, Unknown = -1, Utf8 = 1, Utf16 = 2, Utf32 = 3 }
public async static Task<EncodingType> GetEncodingType(FileStream fs)
{
Byte[] bytes = new Byte[4];
int bytesRead = await fs.ReadAsync(bytes, 0, 4);
if (bytesRead < 2)
return EncodingType.None;
if (bytesRead >= 3 & (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
return EncodingType.Utf8;
if (bytesRead == 4) {
var value = BitConverter.ToUInt32(bytes, 0);
if (value == 0x0000FEFF | value == 0xFEFF0000)
return EncodingType.Utf32;
}
var value16 = BitConverter.ToUInt16(bytes, 0);
if (value16 == (ushort)0xFEFF | value16 == (ushort)0xFFFE)
return EncodingType.Utf16;
return EncodingType.Unknown;
}
}
输出:
Unhandled Exception: System.NotSupportedException: Stream does not support reading. at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state) at System.IO.Stream.<>c.b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state) at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, 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) in C:\Work\docs\program.cs:line 26 at Example.Main() in C:\Work\docs\program.cs:line 13 at Example. ()
示例3: GetEncodingType
public static async Task<EncodingType> GetEncodingType(FileStream fs)
{
if (!fs.CanRead)
return EncodingType.Unknown;
Byte[] bytes = new Byte[4];
int bytesRead = await fs.ReadAsync(bytes, 0, 4);
if (bytesRead < 2)
return EncodingType.None;
if (bytesRead >= 3 & (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
return EncodingType.Utf8;
if (bytesRead == 4) {
var value = BitConverter.ToUInt32(bytes, 0);
if (value == 0x0000FEFF | value == 0xFEFF0000)
return EncodingType.Utf32;
}
var value16 = BitConverter.ToUInt16(bytes, 0);
if (value16 == (ushort)0xFEFF | value16 == (ushort)0xFFFE)
return EncodingType.Utf16;
return EncodingType.Unknown;
}
}
输出:
Filename: .\TestFile.dat, Encoding: Unknown