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


C# FileStream.Read方法代码示例

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


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

示例1: Upload

        public String Upload(UploadTask task)
        {
            FtpWebRequest ftpRequest = null;
            Stream ftpStream = null;
            int bufferSize = 8192;

            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(Host + "/" + task.Remote);
            ftpRequest.Credentials = new NetworkCredential(Login, Password);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

            ftpStream = ftpRequest.GetRequestStream();
            var localFileStream = new FileStream(task.Local, FileMode.Open);
            var byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            while (bytesSent != 0)
            {
                ftpStream.Write(byteBuffer, 0, bytesSent);
                bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;

            return String.Format("{0}", task.Name);
        }
开发者ID:soramusoka,项目名称:gbn-parser,代码行数:28,代码来源:FTPUploader.cs

示例2: GetMimeFromFile

    /// <summary>
    /// Detect MimeType from binary file by reading the first 256 bytes.
    /// </summary>
    /// <param name="filename">filename</param>
    /// <returns>mimetype or unknown/unknown</returns>
    public string GetMimeFromFile(string filename)
    {
      if (!File.Exists(filename))
        throw new FileNotFoundException(String.Format("MimeTypeDetector: '{0}' not found!", filename));

      byte[] buffer = new byte[256];
      using (FileStream fs = new FileStream(filename, FileMode.Open))
      {
        if (fs.Length >= 256)
          fs.Read(buffer, 0, 256);
        else
          fs.Read(buffer, 0, (int)fs.Length);
      }
      try
      {
        System.UInt32 mimetype;
        FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
        System.IntPtr mimeTypePtr = new IntPtr(mimetype);
        string mime = Marshal.PtrToStringUni(mimeTypePtr);
        Marshal.FreeCoTaskMem(mimeTypePtr);
        return mime;
      }
      catch (Exception e)
      {
        return "unknown/unknown";
      }
    }
开发者ID:joconno4,项目名称:MediaPortal-2,代码行数:32,代码来源:MimeTypeDetector.cs

示例3: ExportTxtByData

        /// <summary>
        /// 分断读取数据
        /// </summary>
        /// <param name="path"></param>
        private void ExportTxtByData(string path)
        {
            Response.Clear();

            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + dlfile);
            Response.Flush();

            Stream s = Response.OutputStream;
            using (FileStream fs = new FileStream(path + dlfile, FileMode.Open, FileAccess.Read))
            {
                long fileLength = fs.Length;
                byte[] data = new byte[1024 * 1024];
                int currentPosition = 0;

                while ((fs.Read(data, 0, data.Length) > 0))
                {
                    s.Write(data, 0, data.Length);
                    currentPosition += data.Length;
                    if ((fileLength - currentPosition) > data.Length)
                    {
                        Response.Flush();
                        continue;
                    }
                    else
                    {
                        data = new byte[fileLength - currentPosition];
                        fs.Read(data, 0, data.Length);
                        s.Write(data, 0, data.Length);
                        Response.Flush();
                    }
                }
            }
            s.Close();
        }
开发者ID:zjk537,项目名称:ModelCollect,代码行数:39,代码来源:DlFile.aspx.cs

示例4: Main

        /*
         *
        ushort MachoNetVersion = 320;
        double EVEVersionNumber = 7.31;
        int EVEBuildVersion = 360229;
        string EVEProjectCodename = "EVE-EVE-TRANQUILITY";
        string EVEProjectRegion = "ccp";
        string EVEProjectVersion = "[email protected]";
         */
        static void Main()
        {
            byte GzipMarker = 126; // 0x7E
            // Has 60 bytes worth of raw packet data
            FileStream stream = new FileStream("ExportedStamp", FileMode.Open);
            int PacketLengthMarker = 0;
            ushort Machonet = 0;

            // Resize for Int32 Buffer
            byte[] buffer = new byte[4];
            stream.Read(buffer, 0, 4);
            // It's a packet length in worth of data (Purpose of GZip,
            // otherwise it'd be retarded since TCP protocol already covered that.)
            PacketLengthMarker = BitConverter.ToInt32(buffer, 0);

            // Gzip Stream isn't necessary, because it already extracted via 7E marker.
            //var fixStream = new DeflateStream(stream, CompressionMode.Decompress);
            stream.Read(buffer, 0, 1); // This is 7E marker

            stream.Read(buffer, 0, 4); // This is the unknown marker that occupy 4 bytes of the marshal stream

            buffer = new byte[2];
            stream.Read(buffer, 0, 2);
            Machonet = BitConverter.ToUInt16(buffer, 0);
            //Machonet = BitConverter.ToUInt16(buffer, 0);
            Console.WriteLine("Packet Size: " + PacketLengthMarker);
            Console.WriteLine("Machonet: " + Machonet);
            Console.ReadLine ();
        }
开发者ID:CPPTard,项目名称:EvemuSharp,代码行数:38,代码来源:Main.cs

示例5: ReadValue

        public override void ReadValue(FileStream dataStream)
        {
            // null
            base.ReadValue(dataStream);
            // data buffer
            byte[] buffer = new byte[sizeof(short)];

            // Read our data into the buffer
            dataStream.Read(buffer, 0, sizeof(short));

            // Check Endian
            if (BitConverter.IsLittleEndian)
                Array.Reverse(buffer);

            short first2 = BitConverter.ToInt16(buffer, 0);
            if (first2 < 0)
                throw new Exception("Bad VARCHAR read");

            buffer = new byte[first2];

            dataStream.Read(buffer, 0, first2);

            dataStream.Seek(this.size - first2 - sizeof(short), SeekOrigin.Current);

            Value = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
        }
开发者ID:TeamBrett,项目名称:FadePlus-Master,代码行数:26,代码来源:VarCharField.cs

示例6: Load

        public static CompressedImage Load(string path, int DCTSize)
        {
            var result = new CompressedImage();
            using (var sr = new FileStream(path, FileMode.Open))
            {
                byte[] buffer = new byte[4];

                sr.Read(buffer, 0, 4);
                result.Height = BitConverter.ToInt32(buffer, 0);

                sr.Read(buffer, 0, 4);
                result.Width = BitConverter.ToInt32(buffer, 0);

                sr.Read(buffer, 0, 4);
                result.CompressionLevel = BitConverter.ToInt32(buffer, 0);

                sr.Read(buffer, 0, 4);
                var blockSize = result.FrequencesPerBlock = BitConverter.ToInt32(buffer, 0);

                var blocksCount = result.Height * result.Width / (DCTSize * DCTSize);
                result.Frequences = new List<double>(blocksCount * result.FrequencesPerBlock);

                for (int blockNum = 0; blockNum < blocksCount; blockNum++)
                {
                    for (int freqNum = 0; freqNum < blockSize; freqNum++)
                    {
                        sr.Read(buffer, 0, 2);
                        result.Frequences.Add(BitConverter.ToInt16(buffer, 0));
                    }
                }
            }
            return result;
        }
开发者ID:dantre,项目名称:Shpora_Jpeg,代码行数:33,代码来源:CompressedImage.cs

示例7: GetBuildDateTime

        public static DateTime GetBuildDateTime()
        {
            var assembly = Assembly.GetCallingAssembly();

            if (File.Exists(assembly.Location))
            {
                var buffer = new byte[Math.Max(Marshal.SizeOf(typeof (_IMAGE_FILE_HEADER)), 4)];
                using (var fileStream = new FileStream(assembly.Location, FileMode.Open, FileAccess.Read))
                {
                    fileStream.Position = 0x3C;
                    fileStream.Read(buffer, 0, 4);
                    fileStream.Position = BitConverter.ToUInt32(buffer, 0); // COFF header offset
                    fileStream.Read(buffer, 0, 4); // "PE\0\0"
                    fileStream.Read(buffer, 0, buffer.Length);
                }
                var pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                try
                {
                    var coffHeader = (_IMAGE_FILE_HEADER) Marshal.PtrToStructure(pinnedBuffer.AddrOfPinnedObject(), typeof (_IMAGE_FILE_HEADER));
                    return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1) + new TimeSpan(coffHeader.TimeDateStamp*TimeSpan.TicksPerSecond));
                }
                finally
                {
                    pinnedBuffer.Free();
                }
            }
            return new DateTime();
        }
开发者ID:mike-ward,项目名称:Simply-Weather,代码行数:28,代码来源:BuildInfo.cs

示例8: InitializeSizeAndName

        internal bool InitializeSizeAndName(FileStream fs)
        {
            this.Buffer = new byte[8];
            int bytesRead = fs.Read(this.Buffer, 0, this.Buffer.Length);
            if (bytesRead < this.Buffer.Length)
            {
                return false;
            }

            this.Size = this.GetUInt(0);
            this.Name = this.GetString(4, 4);

            if (this.Size == 0)
            {
                this.Size = (ulong)(fs.Length - fs.Position);
            }

            if (this.Size == 1)
            {
                bytesRead = fs.Read(this.Buffer, 0, this.Buffer.Length);
                if (bytesRead < this.Buffer.Length)
                {
                    return false;
                }

                this.Size = this.GetUInt64(0) - 8;
            }

            this.Position = ((ulong)fs.Position) + this.Size - 8;
            return true;
        }
开发者ID:KatyaMarincheva,项目名称:SubtitleEditOriginal,代码行数:31,代码来源:Box.cs

示例9: FileInitialization

        public Matrix FileInitialization(string filename)
        {
            using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var buff = new byte[255];

                fs.Read(buff, 0, sizeof(int));

                int n = BitConverter.ToInt32(buff, 0);

                fs.Read(buff, 0, sizeof(int));

                int m = BitConverter.ToInt32(buff, 0);

                var matrix = new Matrix(n, m);

                for (var i = 0; i < n; i++)
                {
                    for (var j = 0; j < m; j++)
                    {
                        fs.Read(buff, 0, sizeof (double));
                        matrix.Data[i][j] = BitConverter.ToDouble(buff, 0);
                    }
                }

                return matrix;
            }
        }
开发者ID:Tarakan1992,项目名称:MatrixMultiple,代码行数:28,代码来源:MatrixManager.cs

示例10: LoadUnits

        // ----- ----- ----- PRIVATE ----- ----- -----
        private void LoadUnits(string dir, List<UnitData> list,
            bool createMaster, bool createCommon, bool createUnique)
        {
            string filePath = Path.Combine(dir, "CharacterData");
            using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            using (var reader = new BinaryReader(file)){
                reader.ReadInt32();     // 0x8C == 140固定?
                int charaEndNum = reader.ReadInt32();
                int masterEndNum = reader.ReadInt32();
                int commonEndNum = reader.ReadInt32();
                int uniqueEndNum = reader.ReadInt32();

                if (createMaster) {
                    int count = masterEndNum - 1;
                    var buff = new byte[count * UNIT_SIZE];
                    file.Read(buff, OFFSET, buff.Length);
                    var m = new MemoryStream(buff);
                }
                if (createMaster) {
                    int count = commonEndNum - masterEndNum;
                    var buff = new byte[count * UNIT_SIZE];
                    file.Read(buff, OFFSET + masterEndNum * UNIT_SIZE, buff.Length);
                    var m = new MemoryStream(buff);
                }
                if (createMaster) {
                    int count = uniqueEndNum - commonEndNum;
                    var buff = new byte[count * UNIT_SIZE];
                    file.Read(buff, OFFSET + commonEndNum * UNIT_SIZE, buff.Length);
                    var m = new MemoryStream(buff);
                }
            }
        }
开发者ID:ProjectTane,项目名称:FarenDotNet,代码行数:33,代码来源:UnitDataLoaderNeo.cs

示例11: frmRegHis_Load

        private void frmRegHis_Load(object sender, EventArgs e)
        {
            try
            {
                string strRegAllInfo = File.ReadAllText(strRegKeyFile);


                PzCompression pzCom = PzCompressionHelper.getPzCompression(CompressionType.GZip);
                int nIdx = 0;

                byte[] byTmp;
                using (FileStream fsReg = new FileStream(strRegKeyFile, FileMode.Open, FileAccess.Read))
                {
                    while (true)
                    {
                        byTmp = new byte[4];
                        if (fsReg.Read(byTmp, 0, 4) < 1) break;

                        int nLen = BitConverter.ToInt32(byTmp, 0);
                        byTmp = new byte[nLen];
                        fsReg.Read(byTmp, 0, nLen);

                        textBox1.Text += Encoding.Unicode.GetString(pzCom.DeCompress(byTmp)) + "\r\n";

                        nIdx++;
                    }
                }
                label1.Text = string.Format("共授权完成{0}台机器", nIdx);
            }
            catch (Exception ex)
            {
            }
        }
开发者ID:xy19xiaoyu,项目名称:PatSI,代码行数:33,代码来源:frmRegHis.cs

示例12: UploadWebDavFile

        public int UploadWebDavFile(string _WebFileUrl, string _LocalFile, string _UserName, string _Password)
        {
            try
            {
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)WebRequest.Create(_WebFileUrl);  //Http和服务器交互
                req.Credentials = new NetworkCredential(_UserName, _Password); //验证密码
                req.PreAuthenticate = true;
                req.Method = "PUT";//采用PUT方式
                req.AllowWriteStreamBuffering = true;

                Stream reqStream = req.GetRequestStream();
                FileStream rdm = new FileStream(_LocalFile, FileMode.Open); //打开本地文件流

                byte[] inData = new byte[4096];
                int byteRead = rdm.Read(inData, 0, inData.Length);  //二进制读文件
                while (byteRead > 0)
                {
                    reqStream.Write(inData, 0, byteRead); //响应流写入
                    byteRead = rdm.Read(inData, 0, inData.Length);
                }
                rdm.Close();
                reqStream.Close();

                req.GetResponse(); //提交
            }
            catch
            {
                return 0;
            }
            return 1; //正确返回
        }
开发者ID:skygreen2001,项目名称:Betterlife.Net,代码行数:31,代码来源:TestUtilFtp.cs

示例13: ZipFile

 public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
 {
     if (!File.Exists(FileToZip))
     {
         throw new FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
     }
     FileStream fileStream = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
     FileStream baseOutputStream = File.Create(ZipedFile);
     ZipOutputStream zipOutputStream = new ZipOutputStream(baseOutputStream);
     ZipEntry entry = new ZipEntry("ZippedFile");
     zipOutputStream.PutNextEntry(entry);
     zipOutputStream.SetLevel(CompressionLevel);
     byte[] array = new byte[BlockSize];
     int num = fileStream.Read(array, 0, array.Length);
     zipOutputStream.Write(array, 0, num);
     try
     {
         while ((long)num < fileStream.Length)
         {
             int num2 = fileStream.Read(array, 0, array.Length);
             zipOutputStream.Write(array, 0, num2);
             num += num2;
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     zipOutputStream.Finish();
     zipOutputStream.Close();
     fileStream.Close();
 }
开发者ID:wangshu,项目名称:NY_HACK,代码行数:32,代码来源:clsCompression.cs

示例14: ReadRIFF

        //WriteRIFFStream
        // Function to read a 16 bit/sample mono wave file and return intSampleRate and the sampled data in bytWaveData()
        public bool ReadRIFF(string strFilename, ref int intSampleRate, ref byte[] bytWaveData)
        {
            // returns true if successful, false if not. intSampleRate and bytWaveData updated by reference
            if (!System.IO.File.Exists(strFilename))
                return false;
            try {
                FileStream fs = new FileStream(strFilename, FileMode.Open);
                byte[] bytHeader = new byte[46];
                fs.Read(bytHeader, 0, 46);
                int intFmtChunkLength = System.BitConverter.ToInt32(bytHeader, 16);
                intSampleRate = System.BitConverter.ToInt32(bytHeader, 24);
                int intDataBytes = 0;
                byte[] bytBuffer = new byte[-1 + 1];
                if (intFmtChunkLength == 16) {
                    intDataBytes = System.BitConverter.ToInt32(bytHeader, 40);
                    bytBuffer = new byte[intDataBytes + 44];
                    fs.Read(bytBuffer, 0, intDataBytes + 44);
                    bytWaveData = new byte[intDataBytes];
                    Array.Copy(bytBuffer, 44, bytWaveData, 0, intDataBytes);
                } else if (intFmtChunkLength == 18) {
                    intDataBytes = System.BitConverter.ToInt32(bytHeader, 42);
                    bytBuffer = new byte[intDataBytes + 46];
                    fs.Read(bytBuffer, 0, intDataBytes + 46);
                    bytWaveData = new byte[intDataBytes];
                    Array.Copy(bytBuffer, 46, bytWaveData, 0, intDataBytes);
                }
                fs.Close();

            } catch {
                return false;
            }
            return true;
        }
开发者ID:ptaa32,项目名称:ARDOP,代码行数:35,代码来源:WaveTools.cs

示例15: UploadToFTP

		public static void UploadToFTP(string filePath, string remotePath, string logOn, string password)
		{
			using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				string url = Path.Combine(remotePath, Path.GetFileName(filePath));

				Uri uri = new Uri(url);
				FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(uri);

				ftp.Credentials = new NetworkCredential(logOn, password);
				ftp.KeepAlive = false;
				ftp.Method = WebRequestMethods.Ftp.UploadFile;
				ftp.UseBinary = true;
				ftp.ContentLength = fs.Length;
				ftp.Proxy = null;
				fs.Position = 0;

				int contentLen;
				int buffLength = 2048;
				byte[] buff = new byte[buffLength];

				using (Stream strm = ftp.GetRequestStream())
				{
					contentLen = fs.Read(buff, 0, buffLength);

					while (contentLen != 0)
					{
						strm.Write(buff, 0, contentLen);
						contentLen = fs.Read(buff, 0, buffLength);
					}
				}
			}
		}
开发者ID:ChiangHanLung,项目名称:PIC_VDS,代码行数:33,代码来源:File.cs


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