当前位置: 首页>>代码示例>>C#>>正文


C# System.IO.FileStream.Unlock方法代码示例

本文整理汇总了C#中System.IO.FileStream.Unlock方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.FileStream.Unlock方法的具体用法?C# System.IO.FileStream.Unlock怎么用?C# System.IO.FileStream.Unlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.FileStream的用法示例。


在下文中一共展示了System.IO.FileStream.Unlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseRaw

        public void ParseRaw(byte[] b, int length)
        {
            if (rawData)
            {
                if (length < 0)
                    throw new System.ArgumentOutOfRangeException("length has to be above zero");

                // Do we have content left that we need to convert?
                if (this.received.Length > 0)
                {
                    byte[] old = this.Encoding.GetBytes(this.received);
            #if !COMPACT_FRAMEWORK
                    long size = (long)length + old.LongLength;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.LongLength);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.LongLength, (long)length);
            #else
                    int size = length + old.Length;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.Length);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.Length, length);
            #endif
                    b = tmp;
                    length += old.Length;
                    received = string.Empty;
                }

                // Do we have a working byte array?
                if (b != null && length != 0)
                {
                    BinaryMessage conMsg = new BinaryMessage(trans, b, length);
                    // Plugin handling here
                    FmdcEventArgs e = new FmdcEventArgs(Actions.CommandIncomming, conMsg);
                    MessageReceived(trans, e);
                    if (!e.Handled)
                    {
                        if (this.download)
                        {
                            if (trans.DownloadItem != null && trans.CurrentSegment.Index != -1)
                            {
                                if (trans.CurrentSegment.Length < length)
                                {
                                    trans.Disconnect("You are sending more then i want.. Why?!");
                                    return;
                                }

                                if (trans.CurrentSegment.Position == 0 && !Utils.FileOperations.PathExists(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                {
                                    Utils.FileOperations.AllocateFile(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), trans.DownloadItem.ContentInfo.Size);
                                }

                                // Create the file.
                                //using (System.IO.FileStream fs = System.IO.File.OpenWrite(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                using (System.IO.FileStream fs = new System.IO.FileStream(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write))
                                {
                                    try
                                    {
                                        // Lock this segment of file
                                        fs.Lock(trans.CurrentSegment.Start, trans.CurrentSegment.Length);
                                        // Set position
                                        fs.Position = trans.CurrentSegment.Start + trans.CurrentSegment.Position;
                                        // Write this byte array to file
                                        fs.Write(b, 0, length);
                                        trans.CurrentSegment.Position += length;
                                        // Saves and unlocks file
                                        fs.Flush();
                                        fs.Unlock(trans.CurrentSegment.Start, trans.CurrentSegment.Length);
                                    }
                                    catch (System.Exception exp)
                                    {
                                        //trans.DownloadItem.Cancel(trans.CurrentSegment.Index, trans.Source);
                                        trans.Disconnect("Exception thrown when trying to write to file: " + exp.ToString());
                                        return;
                                    }
                                    finally
                                    {
                                        fs.Dispose();
                                        fs.Close();
                                    }
                                    if (trans.CurrentSegment.Position >= trans.CurrentSegment.Length)
                                    {
                                        trans.DownloadItem.Finished(trans.CurrentSegment.Index, trans.Source);
                                        //// Searches for a download item and a segment id
                                        // Request new segment from user. IF we have found one. ELSE disconnect.
                                        if (GetSegment(true))
                                        {
                                            OnDownload();
                                        }
                                        else
                                            trans.Disconnect("All content downloaded");
                                    }
                                }
                            }
                        }
                        else
                        {
//.........这里部分代码省略.........
开发者ID:BGCX261,项目名称:zpoc3-svn-to-git,代码行数:101,代码来源:transfernmdcprotocol.cs


注:本文中的System.IO.FileStream.Unlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。