c#操作二進制2

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            //存放工作路徑
            string suff = "*.pwv";
            string folderPath = "e:/copy/20120803_11";
            string folderOutPath = "e:/copy/20120803_12";
            
            /*
            string suff = "*.jpg";
            string folderPath = "e:/copy/test1";
            string folderOutPath = "e:/copy/test2";
             * */
            List<string> list = new List<string>();
             DirectoryInfo di = new DirectoryInfo(folderPath);
            FileInfo[] fiels = di.GetFiles(suff, SearchOption.AllDirectories);
            foreach (FileInfo fi in fiels)
            {
                Console.WriteLine(fi.DirectoryName + "/" + fi.Name);
                list.Add(fi.Name);
            }


           foreach(string path in list){
                    FileStream fs, outfile; //聲明FileStream對象
                    try
                    {
                        //讀文件流
                        fs = new FileStream(folderPath+"/"+path, FileMode.OpenOrCreate,FileAccess.Read);
                        //寫文件流
                        outfile = new FileStream(folderOutPath+"/"+path, FileMode.OpenOrCreate, FileAccess.Write);
                        BinaryWriter bw = new BinaryWriter(outfile);

                        var len = (int)fs.Length;
                        var bits = new byte[len];
                        fs.Read(bits, 0, len);
                        //修改二進制內容
                        Random r = new Random();
                        for (int i = 0; i < len; i++) {
                            if (i%4 == 0)
                            {
                                int r1 = r.Next(2);
                                if (r1 == 1)
                                {
                                    if (i + 4 < len)
                                    {
                                        bits[i++] = 0;
                                        bits[i++] = 0;
                                        bits[i++] = 0;
                                        bits[i++] = 0;
                                    }
                                }
                            }
                        }
                        //寫入文件
                        bw.Write(bits);
                        //關閉流
                        bw.Close();
                        outfile.Close(); //關閉BinaryWriter對象
                        fs.Close(); //關閉文件流
                        Console.WriteLine("成功寫入");
                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
           }
        }


        static void process()
        {
          
 
        //// 1  
        ////壓縮c:/freezip/free.txt(即文件夾及其下文件freezip/free.txt)  
        ////到c:/freezip/free.rar  
       /*
        * strzipPath = "E://Mailer.rar";//默認壓縮方式爲 .rar  
           Process1.StartInfo.Arguments = " a -r /"" + strzipPath + "/" " + strtxtPath;
            System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
            Process1.StartInfo.FileName = "Winrar.exe";
            Process1.StartInfo.CreateNoWindow = true;
        * Process1.StartInfo.Arguments = " a -r \"" + strzipPath + "\" " + "e:/copy/test1";
        * */
            
        string strtxtPath = "E://copy/test1";  
        string strzipPath = "E://Mailer.rar";  
        System.Diagnostics.Process Process1 = new System.Diagnostics.Process();  
        Process1.StartInfo.FileName = "Winrar.exe";  
        Process1.StartInfo.CreateNoWindow = true;  
 
        //// 1  
        ////壓縮c:/freezip/free.txt(即文件夾及其下文件freezip/free.txt)  
        ////到c:/freezip/free.rar  
        strzipPath = "E://Mailer.rar";//默認壓縮方式爲 .rar  
        Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;

        Process1.Start();
        if (Process1.HasExited)
        {
            int iExitCode = Process1.ExitCode;
            if (iExitCode == 0)
            {
                Console.Write(iExitCode.ToString() + " 正常完成");
            }
            else
            {
                Console.Write(iExitCode.ToString() + " 有錯完成");
            }
        }  
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章