一 文件操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 讀取整理 { class Program { static string desiteFile = "G:\\任務\\整理1\\";//分類存取的文件 // static string desiteFile = ""; static string path2 = "G:\\任務\\整理1\\result.txt";//結果文件 static string path_config = "G:\\任務\\整理\\audioConfig.txt";//映射文件 static System.IO.StreamWriter sw = new System.IO.StreamWriter(path2, false); static int count = 1; static int countfile = 0; public class Map { public int num ; public string[] name;// new string[10]; public Map() { num = 0; name = new string[10]; } }; static Map[] map = new Map[254]; //讀入映射文件,為聲音文件做對應 做准備 public static void readData() { for (int i = 0; i < 254; i++) map[i] = new Map(); System.IO.FileStream fs = new System.IO.FileStream(path_config, System.IO.FileMode.Open); System.IO.StreamReader m_streamReader = new System.IO.StreamReader(fs); m_streamReader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin); //string arry = ""; string strLine = m_streamReader.ReadLine(); int tmp1 = 1; int tmp2 = 0; do { string[] split = strLine.Split('='); string a = split[0]; tmp1 = System.Int32.Parse(a); if (tmp1 == tmp2) { map[tmp1].num ++; } map[tmp1].name[map[tmp1].num] = split[1]; tmp2 = tmp1; strLine = m_streamReader.ReadLine(); } while (strLine != null && strLine != ""); m_streamReader.Close(); m_streamReader.Dispose(); fs.Close(); fs.Dispose(); } //遞歸 有問題,出現重復復制,重復訪問 public static void read(string path) { //目錄下的文件夾集合 string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories); //目錄下的文件 string[] rootfileArr = System.IO.Directory.GetFiles(path); //文件夾遞歸 for (int i = 0; i < diArr.Length; i++) { //增加父節點 string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]); Console.WriteLine(fileName); //node.Nodes.Add(fileName); read(diArr[i]); } //文件直接添加 foreach (string var in rootfileArr) { string str =""; //標注 string extension = System.IO.Path.GetExtension(var);//擴展名 “.aspx” if(extension == ".wav") { // Console.WriteLine(var); if (count % 10000 == 0 || count == 1) { desiteFile = "G:\\任務\\整理1\\"; desiteFile += countfile.ToString()+"\\"; countfile++; } string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(var);// 沒有擴展名的文件名 “Default” int now = System.Int32.Parse(fileNameWithoutExtension);//獲取標號 for (int i = 0; i <= map[now].num;i++ ) { string strr = count.ToString() + "="+ map[now].name[i]; sw.WriteLine(strr); } str = desiteFile + count.ToString() + extension; System.IO.File.Copy(var, str,true); count++; // Console.WriteLine(count); } } } //非遞歸,直接獲得所有文件名 public static void read_new(string path) { //目錄下的文件夾集合 string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories); //目錄下的文件 //文件夾遞歸 for (int i = 0; i < diArr.Length; i++) { //增加父節點 string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]); Console.WriteLine(fileName); //node.Nodes.Add(fileName); // read(diArr[i]); string[] rootfileArr = System.IO.Directory.GetFiles(diArr[i]); //文件直接添加 foreach (string var in rootfileArr) { string str = ""; //標注 string extension = System.IO.Path.GetExtension(var);//擴展名 “.aspx” if (extension == ".wav") { // Console.WriteLine(var); if (count % 10000 == 0 || count == 1) { desiteFile = "G:\\任務\\整理1\\"; desiteFile += countfile.ToString() + "\\"; countfile++; } string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(var);// 沒有擴展名的文件名 “Default” int now = System.Int32.Parse(fileNameWithoutExtension);//獲取標號 str = desiteFile +"bjx"+ count.ToString("00000000") + extension; for (int k = 0; k <= map[now].num; k++) { string strr = "bjx" + count.ToString("00000000") + extension + "=" + map[now].name[k]; sw.WriteLine(strr); } System.IO.File.Copy(var, str, true); count++; // Console.WriteLine(count); } } } } static void Main(string[] args) { string path = "G:\\任務\\重命名加標注\\";//讀取的目錄文件 readData(); read_new(path); sw.Close();// } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 讀取整理 { class Program { static string desiteFile = "G:\\任務\\整理1\\";//分類存取的文件 // static string desiteFile = ""; static string path2 = "G:\\任務\\整理1\\result.txt";//結果文件 static string path_config = "G:\\任務\\整理\\audioConfig.txt";//映射文件 static System.IO.StreamWriter sw = new System.IO.StreamWriter(path2, false); static int count = 1; static int countfile = 0; public class Map { public int num ; public string[] name;// new string[10]; public Map() { num = 0; name = new string[10]; } }; static Map[] map = new Map[254]; //讀入映射文件,為聲音文件做對應 做准備 public static void readData() { for (int i = 0; i < 254; i++) map[i] = new Map(); System.IO.FileStream fs = new System.IO.FileStream(path_config, System.IO.FileMode.Open); System.IO.StreamReader m_streamReader = new System.IO.StreamReader(fs); m_streamReader.BaseStream.Seek(0, System.IO.SeekOrigin.Begin); //string arry = ""; string strLine = m_streamReader.ReadLine(); int tmp1 = 1; int tmp2 = 0; do { string[] split = strLine.Split('='); string a = split[0]; tmp1 = System.Int32.Parse(a); if (tmp1 == tmp2) { map[tmp1].num ++; } map[tmp1].name[map[tmp1].num] = split[1]; tmp2 = tmp1; strLine = m_streamReader.ReadLine(); } while (strLine != null && strLine != ""); m_streamReader.Close(); m_streamReader.Dispose(); fs.Close(); fs.Dispose(); } //遞歸 有問題,出現重復復制,重復訪問 public static void read(string path) { //目錄下的文件夾集合 string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories); //目錄下的文件 string[] rootfileArr = System.IO.Directory.GetFiles(path); //文件夾遞歸 for (int i = 0; i < diArr.Length; i++) { //增加父節點 string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]); Console.WriteLine(fileName); //node.Nodes.Add(fileName); read(diArr[i]); } //文件直接添加 foreach (string var in rootfileArr) { string str =""; //標注 string extension = System.IO.Path.GetExtension(var);//擴展名 “.aspx” if(extension == ".wav") { // Console.WriteLine(var); if (count % 10000 == 0 || count == 1) { desiteFile = "G:\\任務\\整理1\\"; desiteFile += countfile.ToString()+"\\"; countfile++; } string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(var);// 沒有擴展名的文件名 “Default” int now = System.Int32.Parse(fileNameWithoutExtension);//獲取標號 for (int i = 0; i <= map[now].num;i++ ) { string strr = count.ToString() + "="+ map[now].name[i]; sw.WriteLine(strr); } str = desiteFile + count.ToString() + extension; System.IO.File.Copy(var, str,true); count++; // Console.WriteLine(count); } } } //非遞歸,直接獲得所有文件名 public static void read_new(string path) { //目錄下的文件夾集合 string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories); //目錄下的文件 //文件夾遞歸 for (int i = 0; i < diArr.Length; i++) { //增加父節點 string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]); Console.WriteLine(fileName); //node.Nodes.Add(fileName); // read(diArr[i]); string[] rootfileArr = System.IO.Directory.GetFiles(diArr[i]); //文件直接添加 foreach (string var in rootfileArr) { string str = ""; //標注 string extension = System.IO.Path.GetExtension(var);//擴展名 “.aspx” if (extension == ".wav") { // Console.WriteLine(var); if (count % 10000 == 0 || count == 1) { desiteFile = "G:\\任務\\整理1\\"; desiteFile += countfile.ToString() + "\\"; countfile++; } string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(var);// 沒有擴展名的文件名 “Default” int now = System.Int32.Parse(fileNameWithoutExtension);//獲取標號 str = desiteFile +"bjx"+ count.ToString("00000000") + extension; for (int k = 0; k <= map[now].num; k++) { string strr = "bjx" + count.ToString("00000000") + extension + "=" + map[now].name[k]; sw.WriteLine(strr); } System.IO.File.Copy(var, str, true); count++; // Console.WriteLine(count); } } } } static void Main(string[] args) { string path = "G:\\任務\\重命名加標注\\";//讀取的目錄文件 readData(); read_new(path); sw.Close();// } } }
二 二進制文件操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace read { class Program { static void read_correct(string file1) { /* * 函數: * 功能:校正文件數據值 * 時間:2013、8、8 * */ //讀取文件流 System.IO.FileStream fs1 = new System.IO.FileStream(file1, System.IO.FileMode.Open); System.IO.BinaryReader m_streamReader = new System.IO.BinaryReader(fs1); Byte[] head = new Byte[44]; int count = m_streamReader.Read(head,0,44);//讀取頭部 Int16[] data = new Int16[1000000]; Int16 temp; int countnum =0; int sum=0; fs1.Position = 44; //循環讀取並求和 while (fs1.Length > fs1.Position) { temp = m_streamReader.ReadInt16(); sum+=temp; data[countnum] = temp; countnum++; } //關閉讀取文件 m_streamReader.Close(); m_streamReader.Dispose(); fs1.Close(); //打開寫入文件 System.IO.FileStream fs2 = new System.IO.FileStream(file1, System.IO.FileMode.Open); System.IO.BinaryWriter sw = new System.IO.BinaryWriter(fs2); sw.Write(head);//寫入頭部 fs2.Position = 44; int mean = sum / countnum; //數據校正,每個值減去均值 for(int i=0; i < countnum;i++) { data[i] -= (Int16)mean; sw.Write(data[i]); } //關閉文件及流 sw.Close(); fs2.Dispose(); } static int count = 0; /* * 函數: * 功能:循環讀取各個文件夾,遍歷wav文件 * 時間:2013、8、8 * */ public static void read_new(string path1) { //目錄下的文件夾集合 string[] diArr = System.IO.Directory.GetDirectories(path1, "*", System.IO.SearchOption.AllDirectories); //目錄下的文件 string desiteFile = "G:\\任務\\整理2\\"; //文件夾遞歸 for (int i = 0; i < diArr.Length; i++) { //增加父節點 string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]); Console.WriteLine(fileName); string[] rootfileArr = System.IO.Directory.GetFiles(diArr[i]); //文件直接添加 foreach (string var in rootfileArr) { string extension = System.IO.Path.GetExtension(var);//擴展名 “.aspx” if (extension == ".wav") { read_correct(var); } } } } static void Main(string[] args) { string path1 = "G:\\任務\\整理1"; read_new(path1); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace read { class Program { static void read_correct(string file1) { /* * 函數: * 功能:校正文件數據值 * 時間:2013、8、8 * */ //讀取文件流 System.IO.FileStream fs1 = new System.IO.FileStream(file1, System.IO.FileMode.Open); System.IO.BinaryReader m_streamReader = new System.IO.BinaryReader(fs1); Byte[] head = new Byte[44]; int count = m_streamReader.Read(head,0,44);//讀取頭部 Int16[] data = new Int16[1000000]; Int16 temp; int countnum =0; int sum=0; fs1.Position = 44; //循環讀取並求和 while (fs1.Length > fs1.Position) { temp = m_streamReader.ReadInt16(); sum+=temp; data[countnum] = temp; countnum++; } //關閉讀取文件 m_streamReader.Close(); m_streamReader.Dispose(); fs1.Close(); //打開寫入文件 System.IO.FileStream fs2 = new System.IO.FileStream(file1, System.IO.FileMode.Open); System.IO.BinaryWriter sw = new System.IO.BinaryWriter(fs2); sw.Write(head);//寫入頭部 fs2.Position = 44; int mean = sum / countnum; //數據校正,每個值減去均值 for(int i=0; i < countnum;i++) { data[i] -= (Int16)mean; sw.Write(data[i]); } //關閉文件及流 sw.Close(); fs2.Dispose(); } static int count = 0; /* * 函數: * 功能:循環讀取各個文件夾,遍歷wav文件 * 時間:2013、8、8 * */ public static void read_new(string path1) { //目錄下的文件夾集合 string[] diArr = System.IO.Directory.GetDirectories(path1, "*", System.IO.SearchOption.AllDirectories); //目錄下的文件 string desiteFile = "G:\\任務\\整理2\\"; //文件夾遞歸 for (int i = 0; i < diArr.Length; i++) { //增加父節點 string fileName = System.IO.Path.GetFileNameWithoutExtension(diArr[i]); Console.WriteLine(fileName); string[] rootfileArr = System.IO.Directory.GetFiles(diArr[i]); //文件直接添加 foreach (string var in rootfileArr) { string extension = System.IO.Path.GetExtension(var);//擴展名 “.aspx” if (extension == ".wav") { read_correct(var); } } } } static void Main(string[] args) { string path1 = "G:\\任務\\整理1"; read_new(path1); } } }