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


C# FileStream.Write方法代码示例

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


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

示例1: EncodeFile

        static void EncodeFile(byte[] key, String sourceFile, string destFile)
        {
            // initialize keyed hash object
            HMACSHA1 hms = new HMACSHA1(key);

            // open filestreams to read in original file and write out coded file
            using(FileStream inStream = new FileStream(sourceFile,FileMode.Open))
            using (FileStream outStream = new FileStream(destFile, FileMode.Create))
            {
                // array to hold keyed hash value of original file
                byte[] hashValue = hms.ComputeHash(inStream);

                // reset instream ready to read original file contents from start
                inStream.Position = 0;

                // write keyed hash value to coded file
                outStream.Write(hashValue, 0, hashValue.Length);

                // copy data from original file to output file, 1K at a time
                int bytesRead;
                byte[] buffer = new byte[1024];
                do
                {
                    bytesRead = inStream.Read(buffer, 0, 1024);
                    outStream.Write(buffer, 0, bytesRead);
                } while (bytesRead > 0);
                hms.Clear();

                inStream.Close();
                outStream.Close();
            }
        }
开发者ID:BigBearGCU,项目名称:FNDEV-Week10-Cryptography,代码行数:32,代码来源:Program.cs

示例2: ReceiveFile

        private void ReceiveFile(object obj)
        {
            string msg = (string)obj;
            string savePath = msg.Split(',')[0];
            int len = int.Parse(msg.Split(',')[1]);
            FileStream fileReceive = new FileStream(savePath, FileMode.Create, FileAccess.Write);

            int bufferSize = Clients.client_FileTransport.ReceiveBufferSize;
            byte[] buffer = new byte[bufferSize]; //定义缓冲区
            int left = len;
            //接收数据
            while (left > 0)
            {
                if (left > buffer.Length)
                {
                    Clients.netStream_FileTransport.Read(buffer, 0, buffer.Length);
                    fileReceive.Write(buffer, 0, buffer.Length);
                    left -= bufferSize;
                    int value = (bufferSize - left) / len;
                    SetProgress(value);
                }
                else
                {
                    Clients.netStream_FileTransport.Read(buffer, 0, left);
                    fileReceive.Write(buffer, 0, left);
                    left -= bufferSize;
                    SetProgress(100);
                }
            }
            fileReceive.Close();
        }
开发者ID:SaintLoong,项目名称:RemoteControl_Client,代码行数:31,代码来源:DownloadProcessBar.cs

示例3: Encrypt

        public void Encrypt(string key)
        {
            var stream = new FileStream(Filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            // decido quanti byte di inizio file criptare
            // ovviamente devo tener conto della dimensione del file
            int headerSize = 254;
            if (headerSize > stream.Length)
                headerSize = (int)stream.Length;

            var firstBytes = new byte[headerSize];
            stream.Read(firstBytes, 0, firstBytes.Length);

            // crittografa il primo blocco di N bytes
            var crypto = RijndaelService.EncryptBytes(firstBytes, key);

            // leggi tutto il contenuto restante
            byte[] bytes = new byte[stream.Length - firstBytes.Length];
            stream.Read(bytes, 0, bytes.Length);

            stream.Close();
            stream.Dispose();

            // LA situazione ora è definita da
            // crypto: primi N bytes criptati
            // bytes: i restanti bytes del file, normali

            stream = new FileStream(Filename, FileMode.Create);
            // All'inizio del blocco specifica il numero dei byte adibiti all'header section
            stream.Write(BitConverter.GetBytes(crypto.Length), 0, sizeof(int));
            stream.Write(crypto, 0, crypto.Length);
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
            stream.Dispose();
        }
开发者ID:vitodtagliente,项目名称:USBss,代码行数:34,代码来源:FileEncryptService.cs

示例4: EncodeFile

 // Computes a keyed hash for a source file, creates a target file with the keyed hash
 // prepended to the contents of the source file, then decrypts the file and compares
 // the source and the decrypted files.
 public static void EncodeFile(byte[] key, String sourceFile, String destFile)
 {
     // Initialize the keyed hash object.
     HMACSHA256 myhmacsha256 = new HMACSHA256(key);
     FileStream inStream = new FileStream(sourceFile, FileMode.Open);
     FileStream outStream = new FileStream(destFile, FileMode.Create);
     // Compute the hash of the input file.
     byte[] hashValue = myhmacsha256.ComputeHash(inStream);
     // Reset inStream to the beginning of the file.
     inStream.Position = 0;
     // Write the computed hash value to the output file.
     outStream.Write(hashValue, 0, hashValue.Length);
     // Copy the contents of the sourceFile to the destFile.
     int bytesRead;
     // read 1K at a time
     byte[] buffer = new byte[1024];
     do
     {
         // Read from the wrapping CryptoStream.
         bytesRead = inStream.Read(buffer, 0, 1024);
         outStream.Write(buffer, 0, bytesRead);
     } while (bytesRead > 0);
     myhmacsha256.Clear();
     // Close the streams
     inStream.Close();
     outStream.Close();
     return;
 }
开发者ID:shi5588,项目名称:shi5588,代码行数:31,代码来源:Program.cs

示例5: Encrypt

        public void Encrypt(string filename)
        {
            FileStream fsInput = new FileStream(filename, FileMode.Open, FileAccess.Read);
            FileStream fsOutput = new FileStream(filename + ".crypt", FileMode.Create, FileAccess.Write);
            AesCryptoServiceProvider Aes = new AesCryptoServiceProvider();

            Aes.KeySize = 128;
            Aes.GenerateIV();
            Aes.GenerateKey();

            byte[] output = _algorithm_asym.Encrypt(Aes.Key, false);
            fsOutput.Write(output, 0, 256);
            output = _algorithm_asym.Encrypt(Aes.IV, false);
            fsOutput.Write(output, 0, 256);

            ICryptoTransform encrypt = Aes.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsOutput, encrypt, CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length - 1];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

            fsInput.Close();
            fsOutput.Close();
        }
开发者ID:Jupotter,项目名称:virologie,代码行数:25,代码来源:FileEncryption.cs

示例6: BluRaySupWriteAndReadTwoBitmaps

        public void BluRaySupWriteAndReadTwoBitmaps()
        {
            var fileName = Guid.NewGuid() + ".sup";
            using (var binarySubtitleFile = new FileStream(fileName, FileMode.Create))
            {
                var brSub = new BluRaySupPicture
                {
                    StartTime = 0,
                    EndTime = 1000,
                    Width = 1080,
                    Height = 720
                };
                var buffer = BluRaySupPicture.CreateSupFrame(brSub, new Bitmap(100, 20), 10, 25, 10, ContentAlignment.BottomCenter);
                binarySubtitleFile.Write(buffer, 0, buffer.Length);
                brSub.StartTime = 2000;
                brSub.EndTime = 3000;
                buffer = BluRaySupPicture.CreateSupFrame(brSub, new Bitmap(100, 20), 10, 25, 10, ContentAlignment.BottomCenter);
                binarySubtitleFile.Write(buffer, 0, buffer.Length);
            }

            var log = new StringBuilder();
            var subtitles = BluRaySupParser.ParseBluRaySup(fileName, log);

            Assert.AreEqual(2, subtitles.Count);
        }
开发者ID:socialpercon,项目名称:subtitleedit,代码行数:25,代码来源:BluRaySupParserTest.cs

示例7: SignFile

 // Computes a keyed hash for a source file and creates a target file with the keyed hash
 // prepended to the contents of the source file.
 public static void SignFile(byte[] key, String sourceFile, String destFile)
 {
     // Initialize the keyed hash object.
     using (HMACSHA512 hmac = new HMACSHA512(key))
     {
         using (FileStream inStream = new FileStream(sourceFile, FileMode.Open))
         {
             using (FileStream outStream = new FileStream(destFile, FileMode.Create))
             {
                 // Compute the hash of the input file.
                 byte[] hashValue = hmac.ComputeHash(inStream);
                 // Reset inStream to the beginning of the file.
                 inStream.Position = 0;
                 // Write the computed hash value to the output file.
                 outStream.Write(hashValue, 0, hashValue.Length);
                 // Copy the contents of the sourceFile to the destFile.
                 int bytesRead;
                 // read 1K at a time
                 byte[] buffer = new byte[1024];
                 do
                 {
                     // Read from the wrapping CryptoStream.
                     bytesRead = inStream.Read(buffer, 0, 1024);
                     outStream.Write(buffer, 0, bytesRead);
                 } while (bytesRead > 0);
             }
         }
     }
 }
开发者ID:michaljaros84,项目名称:OneBigPlayground,代码行数:31,代码来源:HashExample.cs

示例8: doBackUp

        public void doBackUp()
        {
            try
            {
                string path = @"C:\";
                string newpath = System.IO.Path.Combine(path, "BackUpLocation");
                Directory.CreateDirectory(newpath);

                FileStream fmdf = new FileStream(mdf, FileMode.Open, FileAccess.Read);
                FileStream fldf = new FileStream(ldf, FileMode.Open, FileAccess.Read);
                FileStream fbak = new FileStream(bak, FileMode.Create, FileAccess.Write);

                byte[] fmcontent = new byte[fmdf.Length];
                fmdf.Read(fmcontent, 0, (int)fmdf.Length);
                fbak.Write(fmcontent, 0, (int)fmdf.Length);

                byte[] sym = new byte[3];
                sym[0] = (byte)'$'; sym[1] = (byte)'*'; sym[2] = (byte)'^';
                fbak.Write(sym, 0, 3);

                byte[] flcontent = new byte[fldf.Length];
                fldf.Read(flcontent, 0, (int)fldf.Length);
                fbak.Write(flcontent, 0, (int)fldf.Length);

                fbak.Flush();
                fbak.Close();
                fmdf.Close();
                fldf.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
开发者ID:omid55,项目名称:real_state_manager,代码行数:34,代码来源:BackUpDatabase.cs

示例9: Export

        // Export data
        public static void Export(byte[] data)
        {
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "Dropset Data|*.dat";
                sfd.Title = "Export Dropset Data";
                sfd.AddExtension = true;
                sfd.RestoreDirectory = true;
                sfd.OverwritePrompt = true;

                DialogResult result = sfd.ShowDialog();

                if (result == DialogResult.OK)
                {
                    try
                    {
                        using (FileStream outstream = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write))
                        {
                            outstream.Write(new byte[] { 0x50, 0x50, 0x46, 0x44, 0x53, 0x45 }, 0, 6);
                            outstream.Write(data, 0, data.Length);
                            outstream.Close();
                        }

                        MessageBox.Show("Dropset data exported successfully.", "Export Successful");
                    }
                    catch
                    {
                        MessageBox.Show("An error occured when writing the dropset data.", "Export Unsuccessful");
                    }
                }
            }
        }
开发者ID:nickworonekin,项目名称:PPFDropsetEditor,代码行数:33,代码来源:ImportExport.cs

示例10: WriteString

 public static void WriteString(FileStream fs, string s)
 {
     if (s == null)
         s = "";
     fs.Write(BitConverter.GetBytes((int)s.Length), 0, 4);
     fs.Write(GetBytes(s), 0, s.Length);
 }
开发者ID:CreeperLava,项目名称:ME3Explorer,代码行数:7,代码来源:FileHelpers.cs

示例11: SaveGameSetToFile

        //, GameSet set)
        internal void SaveGameSetToFile(string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                //write SET file header
                fs.Write(BitConverter.GetBytes(this._recordCount), 0, 2);
                foreach (DatabaseContainer db in this.DatabaseContainers)
                {
                    fs.Write(Encoding.GetEncoding(1252).GetBytes(db.Name.PadRight(_rowNameSize, (char) 0x0)), 0, _rowNameSize);
                    fs.Write(BitConverter.GetBytes(Convert.ToInt32(db.Offset)), 0, 4);
                }

                //write SET file header-trailer (9 nulls followed by int filesize). write size after writing DBFs.
                for (int i = 0; i < 13; i++)
                    fs.WriteByte(0x0);

                long fileSizeBookmark = fs.Position - 4;

                //write out DBFs
                foreach (DatabaseContainer db in this.DatabaseContainers)
                {
            #if DEBUG
                    //writes out individual DBFs... easier to inspect SFRAME, etc
                    string path = this._dbfDirectory + "test";
                    Directory.CreateDirectory(path);
                    db.DbfFileObject.WriteAndClose(path);
            #endif
                    db.DbfFileObject.WriteToStream(fs);
                }
                fs.Position = fileSizeBookmark;
                int fileSize = (int) fs.Length;
                fs.Write(BitConverter.GetBytes(fileSize), 0, 4);
            }
        }
开发者ID:Olreich,项目名称:skaa_editor,代码行数:35,代码来源:GameSetFile.cs

示例12: Save

        public void Save(string path)
        {
            using (var sw = new FileStream(path, FileMode.Create))
            {
                var heightBytes = BitConverter.GetBytes(Height);
                sw.Write(heightBytes, 0, 4);

                var widthBytes = BitConverter.GetBytes(Width);
                sw.Write(widthBytes, 0, 4);

                var compressionLevelBytes = BitConverter.GetBytes(CompressionLevel);
                sw.Write(compressionLevelBytes, 0, 4);

                var frequencesPerBlockBytes = BitConverter.GetBytes(FrequencesPerBlock);
                sw.Write(frequencesPerBlockBytes, 0, 4);

                int blockSize = FrequencesPerBlock;
                for (int blockNum = 0; blockNum * blockSize < Frequences.Count; blockNum++)
                {
                    for (int freqNum = 0; freqNum < blockSize; freqNum++)
                    {
                        var portion = BitConverter.GetBytes((short)Frequences[blockNum * blockSize + freqNum]);
                        sw.Write(portion, 0, portion.Length);
                    }
                }
            }
        }
开发者ID:dantre,项目名称:Shpora_Jpeg,代码行数:27,代码来源:CompressedImage.cs

示例13: AddSecretKeytoFile

        //Đưa SecretKey vào File của thuật toán 3DES
        public void AddSecretKeytoFile(string inputFile)
        {
            //Add SecretKey to File;
            if (File.Exists(inputFile))
            {
                FileStream fsOpen = new FileStream(inputFile, FileMode.Open, FileAccess.ReadWrite);
                try
                {
                    byte[] content = new byte[fsOpen.Length];
                    fsOpen.Read(content, 0, content.Length);
                    //string sContent = System.Text.Encoding.UTF8.GetString(content); //noi dung bang string

                    //byte[] plainbytesCheck = System.Text.Encoding.UTF8.GetBytes(sContent);
                    byte[] plainbytesKey = new UTF8Encoding(true).GetBytes(m_EncryptedSecretKey + "\r\n");

                    fsOpen.Seek(0, SeekOrigin.Begin);
                    fsOpen.Write(plainbytesKey, 0, plainbytesKey.Length);
                    fsOpen.Flush();
                    fsOpen.Write(content, 0, content.Length);
                    fsOpen.Flush();
                    fsOpen.Close();
                }
                catch
                {
                    fsOpen.Close();
                }
            }
        }
开发者ID:vuchannguyen,项目名称:lg-py,代码行数:29,代码来源:MyFileCryptography.cs

示例14: AddText

 private static void AddText(FileStream fs, string value)
 {
     byte[] info = new UTF8Encoding(true).GetBytes(value);
     fs.Write(info, 0, info.Length);
     byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
     fs.Write(newline, 0, newline.Length);
 }
开发者ID:maubertin,项目名称:GetImageLocation,代码行数:7,代码来源:Program.cs

示例15: DisplayContent

 private static void DisplayContent(HttpWebResponse response, string id)
 {
     Stream stream = response.GetResponseStream();
     if (stream != null)
     {
         string filePath = "D:\\pic\\" + id + ".png";
         FileStream fs = new FileStream(filePath, FileMode.Create);
         GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress);
         byte[] bytes = new byte[1024];
         int len;
         if ((len = gzip.Read(bytes, 0, bytes.Length)) > 0)
         {
             fs.Write(bytes, 0, len);
         }
         else
         {
             fs.Close();
             File.Delete(filePath);
             throw new Exception();
         }
         while ((len = gzip.Read(bytes, 0, bytes.Length)) > 0)
         {
             fs.Write(bytes, 0, len);
         }
         fs.Close();
     }
 }
开发者ID:HaleLu,项目名称:GetPic,代码行数:27,代码来源:Form1.cs


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