C#保存txt文档注意事项
1.使用FileStream(string path, FileMode mode, FileAccess access, FileShare share)方法时,特别注意输入 path 对应的文档的名称,请勿包含"\ / : * ? " < > |"特殊字符;
2.创建文件夹区分时,请使用"\\"用于区分;
3.在使用FileStream.Write(byte[] array, int offset, int count)方法时,可以使用 "byte[] writedata = System.Text.Encoding.Default.GetBytes(results);"将string转换成需要的byte[]数组;
1 //int length = 0; 2 byte[] input = new byte[]{0x03,0x22,0xF1,0x87}; 3 int length = input.Length; 4 byte[] output = new byte[] { 0x30, 0x30, 0x62, 0x68, 0x63, 0x68, 0x61,0x72,0x6C,0x69,0x65,0x2F }; 5 byte[] data = new byte[]{0x30,0x20,0x50,0x60,0x70,0x40,0x60,0x90 }; 6 string input_str = ""; 7 string output_str = ""; 8 9 //输入数据byte数组转换成hex string 10 for(int i = 0; i < input.Length; i++) 11 { 12 input_str = input_str + input[i].ToString("X2") + " "; 13 } 14 15 //输出数据处理 16 for(int j = 0; j < output.Length; j++) 17 { 18 output_str = output_str + output[j].ToString("X2") + " "; 19 } 20 //string datastring = System.Text.Encoding.Default.GetString(input); 21 int x = 32; 22 int y = x % 7; 23 Console.WriteLine(y); 24 //for(int i = 0; i < length; i++) 25 //{ 26 // //if(input[i] != null) 27 // //{ 28 // data[i] = input[i]; 29 // Console.WriteLine("the data details shows below1:" + input[i]); 30 // Console.WriteLine("the data details shows below2:" + data[i]); 31 32 // //} 33 // //else 34 // //{ 35 // // data[i] = 0x00; 36 // //} 37 //} 38 39 Console.WriteLine("the data details shows below:" + data[7]); 40 //Console.ReadLine(); 41 string filePath; 42 string Firstname = "charlie"; 43 string Secondname = "wang"; 44 string Thirdname = "shuo"; 45 //string filepath = "\\PASS"; 46 //filePath = @"D:\"; 47 filePath = System.Environment.CurrentDirectory + "\\commandlog"; 48 if (!Directory.Exists(filePath)) //如果文件夹不存在则创建文件夹 49 { 50 Directory.CreateDirectory(filePath); //创建文件夹 51 } 52 filePath += "\\" + DateTime.Now.ToString("yyMMdd"); 53 if (!Directory.Exists(filePath)) //如果文件夹不存在则创建文件夹 54 { 55 Directory.CreateDirectory(filePath); //创建文件夹 56 } 57 //string result = @"D:\result.txt"; 58 string filename = "fld_" + Firstname + "_" + Secondname + "_" + Thirdname + "_" + DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.ToString("hhmmss") + ".log"; 59 string result = filePath + "\\" + filename; 60 //string result = @"D:\+ (fld_<" + Firstname + ">_<" + Secondname + ">_<" + Thirdname + ">_<" + DateTime.Now.ToString("yyyyMMdd") + ">_<" + DateTime.Now.ToString("hh:mm:ss") + ">.log"; 61 FileStream fileStream = new FileStream(result, FileMode.Create, FileAccess.Write); 62 //string inputdata = "03 22 F1 87"; 63 //string outputdata = "07 62 F1 87 34 32 35 31"; 64 string ascii_str = ""; 65 ascii_str = HEXToASCII(output_str); 66 67 //输入输出数据hex string拼接 68 string str = "[" + DateTime.Now.ToString("G") + "] " + "command: " + input_str + "\r\n" + "[" + DateTime.Now.ToString("G")+ "] " + "response: " + ascii_str + "\r\n" + "" + "\r\n"; 69 //string stringdata = "hello,world,it is my honor!"; 70 str = str + str; 71 string results = commandlocal_sav(input, output_str); 72 73 string output_1 = "34 68 75 98 65 24 35 78"; 74 results += commandlocal_sav(output, output_1); 75 76 string output_2 = "23 76 48 90 65 43 32 21"; 77 results += commandlocal_sav(data, output_2); 78 79 //string转换成byte数组 80 byte[] writedata = System.Text.Encoding.Default.GetBytes(results); 81 fileStream.Write(writedata, 0, writedata.Length); 82 //fileStream.Write(input, 0, input.Length); 83 fileStream.Flush(); 84 fileStream.Close();