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


C# BinaryWriter.Dispose方法代碼示例

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


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

示例1: EncryptTokenData

        /// <summary>
        /// Encrypts IdentityToken data with the RSA algorithm.
        /// </summary>
        /// <returns>A byte array.</returns>
        public static byte[] EncryptTokenData(this RSA rsa, byte[] dataToEncrypt, string secPolicyUri)
        {
            int cipherTextBlockSize = rsa.KeySize / 8;
            int plainTextBlockSize;
            switch (secPolicyUri)
            {
                case SecurityPolicyUris.Basic128Rsa15:
                    plainTextBlockSize = Math.Max(cipherTextBlockSize - 11, 1);
                    break;

                case SecurityPolicyUris.Basic256:
                    plainTextBlockSize = Math.Max(cipherTextBlockSize - 42, 1);
                    break;

                case SecurityPolicyUris.Basic256Sha256:
                    plainTextBlockSize = Math.Max(cipherTextBlockSize - 42, 1);
                    break;

                default:
                    plainTextBlockSize = 1;
                    break;
            }

            int blockCount = CeilingDivide(dataToEncrypt.Length + 4, plainTextBlockSize);
            int plainTextSize = blockCount * plainTextBlockSize;
            int cipherTextSize = blockCount * cipherTextBlockSize;

            // setup source
            var source = SerializableBytes.CreateWritableStream();
            var writer = new BinaryWriter(source);
            try
            {
                // encode length.
                writer.Write(dataToEncrypt.Length);

                // encode data.
                writer.Write(dataToEncrypt);

                // round up to multiple of plainTextBlockSize
                source.SetLength(plainTextSize);
                source.Seek(0L, SeekOrigin.Begin);

                // setup target
                byte[] cipherText = new byte[cipherTextSize];
                var target = new MemoryStream(cipherText, true);
                try
                {
                    rsa.EncryptStream(source, target, secPolicyUri);
                    return cipherText;
                }
                finally
                {
                    target.Dispose();
                }
            }
            finally
            {
                writer.Dispose();
            }
        }
開發者ID:yuriik83,項目名稱:workstation-uaclient,代碼行數:64,代碼來源:RsaExtensions.cs

示例2: EncodeFile

        public static void EncodeFile(string sourceFilePath, string destinationFilePath)
        {
            FileStream fs_src = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);
            BinaryReader br_src = new BinaryReader(fs_src);
            FileStream fs_dst = new FileStream(destinationFilePath, FileMode.Open, FileAccess.Write);
            BinaryWriter bw_dst = new BinaryWriter(fs_dst);
            string temp;
            ASCIIEncoding asci = new ASCIIEncoding();
            while (fs_src.Length > fs_src.Position)
            {
                temp = string.Empty;
                temp += (char)br_src.ReadByte();
                if (fs_src.Position + 1 < fs_src.Length)
                    temp += (char)br_src.ReadByte();
                if (fs_src.Position + 1 < fs_src.Length)
                    temp += (char)br_src.ReadByte();
                bw_dst.Write(Convert.ToBase64String(asci.GetBytes(temp)));

            }
            bw_dst.Close();
            bw_dst.Dispose();
            br_src.Close();
            br_src.Dispose();
            fs_src.Close();
            fs_src.Dispose();
            fs_dst.Close();
            fs_dst.Dispose();
            temp = null;
        }
開發者ID:jiashida,項目名稱:super-trojan-horse,代碼行數:29,代碼來源:base64.cs

示例3: SendPacket

        public async Task SendPacket(IPacket packet, Stream netStream)
        {
            var ms = new MemoryStream();
            var bw = new BinaryWriter(ms);

            if (packet is IAutoSerializePacket)
                (packet as IAutoSerializePacket).AutoSerialize(bw);
            else
            {
                bw.Write(packet.ID);
                packet.SerializePacket(bw);
            }

            bw.Flush();

            // Copy ms -> redirect writer to new ms -> prepend packet size prefix -> append packet paylod
            FinalizePacket(ref bw);
            ms.Dispose(); // Dispose of expired ms, writer's basestream is created in FinalizePacket
            ms = bw.BaseStream as MemoryStream;
            // this here failed? ye wait a moment
            await netStream.WriteAsync(ms.ToArray(), 0, (int)ms.Length);

            if (OnPacketSent != null)
                OnPacketSent(null, new PacketEventArgs(null, packet, (int)ms.Length));

            ms.Dispose();
            bw.Dispose();

        }
開發者ID:Numbers11,項目名稱:uNet,代碼行數:29,代碼來源:PacketProcessor.cs

示例4: AsymetriycCrypt

        /// <summary>
        /// Metoda za asimetrično kriptiranje
        /// </summary>
        /// <param name="file"></param>
        public void AsymetriycCrypt(string file)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            StreamReader streamReader = new StreamReader("javni_kljuc.txt");
            string publicKey = streamReader.ReadToEnd();
            rsa.FromXmlString(publicKey);
            streamReader.Close();

            string record = file + ".rsa";

            FileStream fstreamU = File.OpenRead(file),
            fstreamO = new FileStream(record, FileMode.Create, FileAccess.ReadWrite);

            BinaryWriter bw = new BinaryWriter(fstreamO);

            BinaryReader binReader = new BinaryReader(fstreamU);
            byte[] bytes = binReader.ReadBytes((int)fstreamU.Length);
            binReader.Close();

            byte[] crypt = rsa.Encrypt(bytes, false);

            bw.Write(crypt);
            bw.Flush();
            bw.Close();
            bw.Dispose();

            fstreamU.Close();
            fstreamU.Dispose();
        }
開發者ID:ivpusic,項目名稱:cryptography_algorithms,代碼行數:33,代碼來源:AsymCryptHelper.cs

示例5: AsymetriycDecrypt

        /// <summary>
        /// Metoda za asimetrično dekriptiranje
        /// </summary>
        /// <param name="file"></param>
        public void AsymetriycDecrypt(string file)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            RSACryptoServiceProvider.UseMachineKeyStore = false;
            StreamReader streamReader = new StreamReader("privatni_kljuc.txt");
            string privateKey = streamReader.ReadToEnd();
            rsa.FromXmlString(privateKey);
            streamReader.Close();

            int indexEcb = file.LastIndexOf(".rsa");
            string zapis = file.Substring(0, indexEcb);

            FileStream fstreamU = File.OpenRead(file),
            fstreamO = new FileStream(zapis, FileMode.Create, FileAccess.ReadWrite);

            BinaryWriter bw = new BinaryWriter(fstreamO);
            BinaryReader binReader = new BinaryReader(fstreamU);
            byte[] bytes = binReader.ReadBytes((int)fstreamU.Length);

            binReader.Close();

            byte[] decrypt = rsa.Decrypt(bytes, false);
            bw.Write(decrypt);

            bw.Flush();
            bw.Close();
            bw.Dispose();

            fstreamU.Close();
            fstreamU.Dispose();
        }
開發者ID:ivpusic,項目名稱:cryptography_algorithms,代碼行數:35,代碼來源:AsymCryptHelper.cs

示例6: Write

        public void Write(string fileName, bool overwrite)
        {
            _stream = new MemoryStream();
            _writer = new BinaryWriter(_stream);

            if (File.Exists(fileName) && !overwrite)
            {
                return;
            }
            
            _writer.Write(_file.Version);
            WriteStringTable();

            WriteBlock(_file.MainBlock);

            string dir = Path.GetDirectoryName(fileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            File.WriteAllBytes(fileName, _stream.ToArray());

            _writer.Close();
            _stream.Close();
            _writer.Dispose();
            _stream.Dispose();
        }
開發者ID:andreberg,項目名稱:TLII.IO,代碼行數:28,代碼來源:AdmWriter.cs

示例7: WriteToFileSystem

        public static void WriteToFileSystem(string filename, byte[] input)
        {
            FileStream fs = null;
            BinaryWriter bw = null;
            try
            {
                fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                bw = new BinaryWriter(fs);
                using (var ms = new MemoryStream(input))
                {
                    bw.Write(ms.ToArray());
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Writing to the path '" + filename + "' failed.", ex);
            }
            finally
            {
                if (bw != null)
                {
                    bw.Close();
                    bw.Dispose();
                }

                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
開發者ID:devoneonline,項目名稱:Tfs-Api,代碼行數:32,代碼來源:StreamUtility.cs

示例8: button3_Click

 private void button3_Click(object sender, EventArgs e)
 {
     BinaryWriter bw = new BinaryWriter(File.OpenWrite(path));
     //bw.Write(textBox1.Text);
     short myshort = 1;
     byte[] buffer = BitConverter.GetBytes(myshort);
     Array.Reverse(buffer);
     bw.Dispose();
 }
開發者ID:Aniruddha-Tapas,項目名稱:C-Sharp-Applications,代碼行數:9,代碼來源:Form1.cs

示例9: WriteBinaryFile

 public static void WriteBinaryFile(List<char> input) {
     FileStream fs = new FileStream("C:\\TEMP\\BINARY.BIN",FileMode.Create, FileAccess.ReadWrite);
     BinaryWriter bw = new BinaryWriter(fs,System.Text.Encoding.UTF8);
     foreach(char i in input) {
         bw.Write(i);
     }
     bw.Flush();
     bw.Close();
     bw.Dispose();
 }
開發者ID:TheCriticalPath,項目名稱:Project-Euler,代碼行數:10,代碼來源:FileHelper.cs

示例10: button2_Click

 private void button2_Click(object sender, EventArgs e)
 {
     BinaryWriter bw = new BinaryWriter(File.OpenWrite(path));
     short myshort = 1;
     byte[] buffer = BitConverter.GetBytes(myshort);
     Array.Reverse(buffer);//convert bytes in the wrong directions
     bw.Write('C');//write just a character.
     bw.Write(1);//write in wrong direction like before.
     bw.Dispose();
 }
開發者ID:ChrisPaine,項目名稱:C-sharp,代碼行數:10,代碼來源:Form1.cs

示例11: FirmTest

 static void FirmTest(string[] args)
 { 
   FwFile myfirm = new FwFile();
   myfirm.Test(args.Length >1 ? args[2] : Directory.GetCurrentDirectory() + @"\rofs3.fpsx");
   string binfile = Directory.GetFiles( Directory.GetCurrentDirectory() + @"\ROFS2", "?_code.bin" )[0];
   UseExternalTools.ExtractImage(Directory.GetCurrentDirectory(), Directory.GetCurrentDirectory() + @"\ROFS2"+binfile, Directory.GetCurrentDirectory() + @"\ROFSROOT");
   if ( File.Exists( "test.hex" ) )
     File.Delete( "test.hex" );
   BinaryWriter bw = new BinaryWriter( new FileStream( "test.hex", FileMode.CreateNew, FileAccess.Write ) );
   bw.Write( ( int ) 0x01020304 );
   bw.Dispose();
 }
開發者ID:flaithbheartaigh,項目名稱:nokias60cenrepeditor,代碼行數:12,代碼來源:Program.cs

示例12: SetExpertMode

 private static void SetExpertMode(string source, string dest, bool expertMode)
 {
     BinaryReader reader = new BinaryReader(new FileStream(source, FileMode.Open));
     int version = reader.ReadInt32();
     if (version < 149) {
         MessageBox.Show("Error: Outdated terraria version");
         return;
     }
     ulong magic = reader.ReadUInt64();
     if ((magic & 72057594037927935uL) != 27981915666277746uL) {
         MessageBox.Show("Error: Invalid header");
         return;
     }
     // Skip other file metadata...
     reader.ReadBytes(12);
     int positionCount = reader.ReadInt16();
     int afterMetadataPos = reader.ReadInt32();
     int afterHeaderPos = reader.ReadInt32();
     // Skip positions...
     reader.ReadBytes((positionCount - 2) * 4);
     // Skip frame importance...
     reader.ReadBytes(reader.ReadInt16() / 8 + 1);
     if (reader.BaseStream.Position != afterMetadataPos) {
         MessageBox.Show("After Metadata Position Mismatch: expected " +
             afterMetadataPos + ", was " + reader.BaseStream.Position);
         return;
     }
     // Skip the first part of the header...
     reader.ReadString();
     reader.ReadInt32();
     reader.ReadInt32();
     reader.ReadInt32();
     reader.ReadInt32();
     reader.ReadInt32();
     reader.ReadInt32();
     reader.ReadInt32();
     // Get the offset...
     long expertModeFlagOffset = reader.BaseStream.Position;
     bool wasExpertMode = reader.ReadBoolean();
     reader.Dispose();
     // Notify the user if the world is changed...
     if (wasExpertMode == expertMode) {
         MessageBox.Show(expertMode ? "World was already Expert Mode." : "World was already not Expert Mode.");
         return;
     }
     BinaryWriter writer = new BinaryWriter(new FileStream(dest, FileMode.Open));
     writer.BaseStream.Position = expertModeFlagOffset;
     writer.Write(expertMode);
     writer.Dispose();
     MessageBox.Show(expertMode ? "World is now Expert Mode!" : "World is no longer Expert Mode!");
 }
開發者ID:antag99,項目名稱:TNoob,代碼行數:51,代碼來源:TNoob.cs

示例13: button1_Click

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Text File|*.txt";//default type
            sfd.FileName = "MyTextFile";// default name
            sfd.Title = "Save Title"; // title name
            if (sfd.ShowDialog() == DialogResult.OK){

                string path = sfd.FileName;
                BinaryWriter bw = new BinaryWriter(File.Create(path));
                bw.Write("This is a test");
                bw.Dispose();
            }
        }
開發者ID:ChrisPaine,項目名稱:C-sharp,代碼行數:14,代碼來源:Form1.cs

示例14: button1_Click

 private void button1_Click(object sender, EventArgs e)
 {
     SaveFileDialog sfd = new SaveFileDialog();
     sfd.Filter = "Text File |*.txt";
     sfd.FileName = "MyTextFile";
     sfd.Title = "Save File Dialog";
     if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         string path = sfd.FileName;
         BinaryWriter bw = new BinaryWriter(File.Create(path));
         bw.Write("New File");
         bw.Dispose();
     }
 }
開發者ID:Aniruddha-Tapas,項目名稱:C-Sharp-Applications,代碼行數:14,代碼來源:Form1.cs

示例15: SaveFeeds

        public static void SaveFeeds(IEnumerable<Feed> feeds) {
            // Save to temp file first in case of error.
            var tempFileName = Path.GetTempFileName();

            FileStream stream = null;
            BinaryWriter writer = null;

            try {
                stream = File.Open(tempFileName, FileMode.Create, FileAccess.Write, FileShare.None);
                writer = new BinaryWriter(stream);

                writer.Write(SaveHeader);
                writer.Write(SaveVersion);

                writer.Write(feeds.Count());
                foreach (var feed in feeds) {
                    writer.Write(feed.Title);
                    writer.Write(feed.Url);
                    writer.Write(feed.SiteUrl);
                    writer.Write(feed.Description ?? String.Empty);
                    writer.Write(feed.Updated.ToBinary());
                    writer.Write(feed.IsPreserved);
                    writer.Write(feed.Category ?? String.Empty);

                    writer.Write(feed.Items.Count);
                    foreach (var item in feed.Items) {
                        writer.Write(item.Title);
                        writer.Write(item.Url);
                        writer.Write(item.PodcastUrl ?? String.Empty);
                        writer.Write(item.Published.ToBinary());
                        writer.Write(item.Summary);
                        writer.Write(item.IsRead);
                    }
                }
            }
            finally {
                if (writer != null) {
                    writer.Dispose();
                }

                if (stream != null) {
                    stream.Dispose();
                }

                // Copy temp file to actual file.
                File.Copy(tempFileName, GetFeedsPath(), overwrite: true);
                File.Delete(tempFileName);
            }
        }
開發者ID:alexmcbride,項目名稱:podcatcherdotnet,代碼行數:49,代碼來源:FeedSerializer.cs


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