當前位置: 首頁>>代碼示例>>C#>>正文


C# File.OpenRead方法代碼示例

本文整理匯總了C#中System.IO.File.OpenRead方法的典型用法代碼示例。如果您正苦於以下問題:C# File.OpenRead方法的具體用法?C# File.OpenRead怎麽用?C# File.OpenRead使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.IO.File的用法示例。


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

示例1: TestSameCore

        private void TestSameCore(File memTempFile, File localTempFile, FileMode mode, FileAccess access, FileShare share)
        {
            Stream memStream = null, localStream = null;
            Exception memException = null, localException = null;
            try
            {
                memStream = memTempFile.Open(mode, access, share);
            }
            catch (Exception e)
            {
                memException = e;
            }
            try
            {
                localStream = localTempFile.Open(mode, access, share);
            }
            catch (Exception e)
            {
                localException = e;
            }
            if (memException != null || localException != null)
            {
                if (memStream != null) memStream.Dispose();
                if (localStream != null) localStream.Dispose();

                var anyException = (memException ?? localException).GetType();

                Assert.That(memException, Is.InstanceOf(anyException), string.Format("In-Memory ex was {0}, but file was: {1}",
                    memException != null ? memException.ToString() : "NULL",
                    localException != null ? localException.ToString() : "NULL"));

                Assert.That(localException, Is.InstanceOf(anyException), string.Format("Local file ex was {0}, but in mem was: {1}.",
                    localException != null ? localException.ToString() : "NULL",
                    memException != null ? memException.ToString() : "NULL"));

                if (!(memException.GetType() == anyException && localException.GetType() == anyException))
                    Console.WriteLine("Memory exception: " + (memException != null ? memException.GetType().Name : null) +
                                      " Local exception: " + (localException != null ? localException.GetType().Name : null));
                return;
            }
            if (memStream.CanWrite)
                memStream.WriteByte(99);
            if (localStream.CanWrite)
                localStream.WriteByte(99);
            memStream.Dispose();
            localStream.Dispose();

            using (memStream = memTempFile.OpenRead())
            using (localStream = localTempFile.OpenRead())
            {
                memStream.ReadToEnd().ShouldBe(localStream.ReadToEnd());
                localStream.Close();
                memStream.Close();
            }
        }
開發者ID:DotNetIO,項目名稱:DotNetIO,代碼行數:55,代碼來源:open_behavior_is_same.cs


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