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() + " 有错完成");
            }
        }  
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值