龙空技术网

c 创建、删除、移动文件(夹),读写txt文件

爱音乐的程序员小新人 132

前言:

现时朋友们对“c语言写txt文件”大概比较关注,咱们都想要分析一些“c语言写txt文件”的相关资讯。那么小编同时在网上搜集了一些对于“c语言写txt文件””的相关资讯,希望咱们能喜欢,看官们一起来了解一下吧!

这里是C#的一个winform小工具。简单说两句

Path.Combine这个可以不用管第一个字符有没有\结尾

创建文件夹

Directory.CreateDirectory(NewFileCrePath);//创造一个新文件夹

创建移动删除文件、文件夹

FileStream fcreate = File.Create(VerPath);//创造一个版本记录文件 irectory.Move(SFPath, ToPath);//ToPath要设置为移动到这里的那个文件夹的名字(或者重命名 Directory.Delete(SFPath, true);

读写TXT: 这里要注意

reader.ReadLine();

每次调用都会读取新的一行哦,这样很方便的一行一行的读取。

此外还有一个

String str = string.Empty; // for (int i = 0; i < 2; i++) // { str = reader.ReadLine();//赋值 // } if (String.IsNullOrEmpty(str) ) { str = reader.ReadLine(); }

谁用谁知道

整体代码

//===================================================文件创建、移动、删除、生成、压缩 private void button5_Click(object sender, EventArgs e) { string VerPath = ""; string FromVerPath = ""; string SFPath = ""; string STemp = "", ToPath = ""; NewFileCrePath = Path.Combine(LPath, SelectDir); try { if (!Directory.Exists(NewFileCrePath)) { Directory.CreateDirectory(NewFileCrePath);//创造一个新文件夹 //MessageBox.Show(sf.ToString()); VerPath = NewFileCrePath + @"\Version.txt"; FileStream fcreate = File.Create(VerPath);//创造一个版本记录文件 fcreate.Close(); for (int sfs = 0; sfs < CountFile; sfs++)//sf是个数,从1开始,sfs是循环计数,从0开始 { STemp = SFiles[sfs]; STemp = STemp.Substring(0, STemp.Length - 4);//去掉文件名称后缀 SFPath = LPath + STemp + @"\" + STemp;//将要移动的文件夹 ToPath = NewFileCrePath + @"\" + STemp;//目标文件夹 if (Directory.Exists(SFPath)) { Directory.Move(SFPath, ToPath);//ToPath要设置为移动到这里的那个文件夹的名字(或者重命名 SFPath = LPath + STemp; Directory.Delete(SFPath, true); } else { MessageBox.Show("要移动的文件不存在!"); return; } //=============================版本生成 FromVerPath = NewFileCrePath + @"\" + STemp + @"\Version.txt"; FileStream fsver = new FileStream(FromVerPath, FileMode.Open);//吧每一个文件的版本提取 StreamReader reader = new StreamReader(fsver, UnicodeEncoding.GetEncoding("GB2312"));//中文、读取 String[] str = new String[5]; for (int i = 0; i < 2; i++) { str[i] = reader.ReadLine();//赋值 } StreamWriter sw = new StreamWriter(VerPath, true, UnicodeEncoding.GetEncoding("GB2312"));//写入 str[0] = STemp + " " +"\t "+ str[0]; sw.WriteLine(str[0]);//在后面写入 sw.Close(); fsver.Close(); }//for循环结束 CountFile = 0; string newzip = NewFileCrePath + ".zip"; ZipDirectory(NewFileCrePath, newzip);//压缩一下 // FileUpLoad(newzip,"");上传一下 } else { MessageBox.Show("要创建的文件夹已经存在!"); } } catch (Exception ex) { MessageBox.Show("error!"); throw ex; } }

标签: #c语言写txt文件 #c语言从txt读取中文 #c语言读取txt #c语言txt文件的读取 #c语言 读取txt