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