本文整理汇总了C#中System.IO.FileStream.BeginRead方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.BeginRead方法的具体用法?C# FileStream.BeginRead怎么用?C# FileStream.BeginRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.BeginRead方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BeginReadThrowsForWriteOnly
public void BeginReadThrowsForWriteOnly()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => fs.BeginRead(new byte[0], 0, 0, null, null));
}
}
示例2: Main
static void Main(string[] args)
{
//AsyncReadOneFile();
//AsyncReadMultiplyFiles();
FileStream fs = new FileStream(@"../../Program.cs", FileMode.Open,
FileAccess.Read, FileShare.Read, 1024,
FileOptions.Asynchronous);
Byte[] data = new Byte[100];
IAsyncResult ar = fs.BeginRead(data, 0, data.Length, null, null);
while (!ar.IsCompleted)
{
Console.WriteLine("Операция не завершена, ожидайте...");
Thread.Sleep(10);
}
Int32 bytesRead = fs.EndRead(ar);
fs.Close();
Console.WriteLine("Количество считаных байт = {0}", bytesRead);
Console.WriteLine(Encoding.UTF8.GetString(data).Remove(0, 1));
}
示例3: BeginReadThrowsForBadOffset
public void BeginReadThrowsForBadOffset(int arraySize, int offset, int numBytes)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<ArgumentException>(() => fs.BeginRead(new byte[arraySize], offset, numBytes, null, null));
}
}
示例4: BeginReadThrowsForNullArray
public void BeginReadThrowsForNullArray()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<ArgumentNullException>("array", () => fs.BeginRead(null, 0, 0, null, null));
}
}
示例5: BeginReadThrowsForNegativeNumBytes
public void BeginReadThrowsForNegativeNumBytes()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<ArgumentOutOfRangeException>("numBytes", () => fs.BeginRead(new byte[0], 0, -1, null, null));
}
}
示例6: APMWithFiles
/// <summary>
/// Demonstrates the use of the APM with files, through the FileStream class.
/// This method performs asynchronous reads and writes to copy data from an input
/// file to an output file. Reads and writes are interlaced, and proceed in chunks
/// of 8KB at a time (displaying progress to the console).
/// </summary>
static void APMWithFiles()
{
FileStream reader = new FileStream("sample.txt", FileMode.Open);
FileStream writer = new FileStream("sample2.txt", FileMode.Create);
byte[] buffer1 = new byte[8192], buffer2 = new byte[8192];
IAsyncResult ar1, ar2 = null;
while (true)
{
ar1 = reader.BeginRead(buffer1, 0, buffer1.Length, null, null);
while (!ar1.IsCompleted)
{
Console.Write("R");
}
if (ar2 != null)
{
while (!ar2.IsCompleted)
{
Console.Write("W");
}
}
int bytesRead;
if ((bytesRead = reader.EndRead(ar1)) == 0)
break; //No more data to read
if (ar2 != null)
{
writer.EndWrite(ar2);
}
Array.Copy(buffer1, buffer2, bytesRead);
ar2 = writer.BeginWrite(buffer2, 0, bytesRead, null, null);
}
Console.WriteLine();
Console.WriteLine();
}
示例7: Main
static void Main(string[] args)
{
// open filestream for asynchronous read
FileStream fs = new FileStream("somedata.dat", FileMode.Open,
FileAccess.Read, FileShare.Read, 1024,
FileOptions.Asynchronous);
// byte array to hold 100 bytes of data
Byte[] data = new Byte[100];
// initiate asynchronous read operation, reading first 100 bytes
IAsyncResult ar = fs.BeginRead(data, 0, data.Length, null, null);
// could do something in here which would run alongside file read...
// check for file read complete
while (!ar.IsCompleted)
{
Console.WriteLine("Operation not completed");
Thread.Sleep(10);
}
// get the result
int bytesRead = fs.EndRead(ar);
fs.Close();
Console.WriteLine("Number of bytes read={0}", bytesRead);
Console.WriteLine(BitConverter.ToString(data, 0, bytesRead));
}
示例8: AsyncReadOneFileCallBackAnonimus
private static void AsyncReadOneFileCallBackAnonimus()
{
Byte[] data = new Byte[100];
Console.WriteLine("Основной поток ID = {0}",
Thread.CurrentThread.ManagedThreadId);
FileStream fs = new FileStream(@"../../Program.cs", FileMode.Open,
FileAccess.Read, FileShare.Read, 1024,
FileOptions.Asynchronous);
fs.BeginRead(data, 0, data.Length, delegate(IAsyncResult ar)
{
Console.WriteLine("Чтение в потоке {0} закончено",
Thread.CurrentThread.ManagedThreadId);
Int32 bytesRead = fs.EndRead(ar);
fs.Close();
Console.WriteLine("Количество считаных байт = {0}", bytesRead);
Console.WriteLine(Encoding.UTF8.GetString(data).Remove(0, 1));
Console.ReadLine();
}, null);
Console.ReadLine();
}
示例9: DownFile
public void DownFile(string fileName)
{
var callback = OperationContext.Current.GetCallbackChannel<IMediaCallBack>();
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
var customFile = new CustomFile(callback, fs, fileName);
fs.BeginRead(customFile.CurrentByte, 0, customFile.MaxLength, DownFileCallBack, customFile);
}
示例10: BeginReadThrowsForClosed
public void BeginReadThrowsForClosed()
{
FileStream fs;
using (fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
}
Assert.Throws<ObjectDisposedException>(() => fs.BeginRead(new byte[0], 0, 0, null, null));
}
示例11: Load
public static void Load(string fileName)
{
startTime = DateTime.Now;
Loaded = 0;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Size = (int)fs.Length;
FileData = new byte[Size];
fs.BeginRead(FileData, 0, Size, new AsyncCallback(read), fs);
}
示例12: CopyFile
public static void CopyFile(String sourcePath, String destinationPath, Action<String, String, Exception> completed)
{
Stream source = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
Stream destination = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[0x1000];
AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(null);
Action<Exception> cbCompleted = e =>
{
if (completed != null) asyncOp.Post(delegate
{
source.Close();
destination.Close();
completed(sourcePath, destinationPath, e);
}, null);
};
AsyncCallback rc = null;
rc = readResult =>
{
try
{
int read = source.EndRead(readResult);
if (read > 0)
{
destination.BeginWrite(buffer, 0, read, writeResult =>
{
try
{
destination.EndWrite(writeResult);
source.BeginRead(
buffer, 0, buffer.Length, rc, null);
}
catch (Exception exc) { cbCompleted(exc); }
}, null);
}
else cbCompleted(null);
}
catch (Exception exc) { cbCompleted(exc); }
};
source.BeginRead(buffer, 0, buffer.Length, rc, null);
}
示例13: Main
static void Main(string[] args)
{
const string FilePath = @"D:\Downloads\Sygic 13.2.2\Sygic Map Downloader (TomTom 2013.06)\sgcmapdownloader.txt";
FileStream fileStream = new FileStream(FilePath,
FileMode.Open, FileAccess.Read, FileShare.Read, 1024,
FileOptions.Asynchronous);
IAsyncResult result = fileStream.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(CompleteRead), fileStream);
Console.ReadLine();
}
示例14: ReplaceFileContent
private void ReplaceFileContent(string file)
{
var fileStream = new FileStream(file, FileMode.Open);
var state = new FileState
{
FileStream = fileStream,
FileName = file,
Data = new byte[fileStream.Length]
};
fileStream.BeginRead(state.Data, 0, (int)fileStream.Length, ReadDone, state);
}
示例15: BeginProcessRequest
public IAsyncResult BeginProcessRequest ( HttpContext context, AsyncCallback cb, object extraData ) {
HttpRequest request = context.Request;
m_response = context.Response;
lock (m_lock) {
m_fileStream = new FileStream(request.MapPath("~/App_Data/TrackerLog.xml"), FileMode.Open, FileAccess.Read, FileShare.Read, 1024, true);
m_xmlFragmentByteArray = new byte[m_fileStream.Length];
return m_fileStream.BeginRead(m_xmlFragmentByteArray, 0, (int)m_fileStream.Length, cb, extraData);
}
}