c语言如何将文件数据删除(c创建删除)

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

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

创建文件夹

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

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

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

读写TXT: 这里要注意

reader.ReadLine();

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

此外还有一个

  1. String str = string.Empty;
  2. // for (int i = 0; i < 2; i )
  3. // {
  4. str = reader.ReadLine();//赋值
  5. // }
  6. if (String.IsNullOrEmpty(str) )
  7. {
  8. str = reader.ReadLine();
  9. }

谁用谁知道

整体代码

  1. //===================================================文件创建、移动、删除、生成、压缩
  2. private void button5_Click(object sender, EventArgs e)
  3. {
  4. string VerPath = "";
  5. string FromVerPath = "";
  6. string SFPath = "";
  7. string STemp = "", ToPath = "";
  8. NewFileCrePath = Path.Combine(LPath, SelectDir);
  9. try
  10. {
  11. if (!Directory.Exists(NewFileCrePath))
  12. {
  13. Directory.CreateDirectory(NewFileCrePath);//创造一个新文件夹
  14. //MessageBox.Show(sf.ToString());
  15. VerPath = NewFileCrePath @"\Version.txt";
  16. FileStream fcreate = File.Create(VerPath);//创造一个版本记录文件
  17. fcreate.Close();
  18. for (int sfs = 0; sfs < CountFile; sfs )//sf是个数,从1开始,sfs是循环计数,从0开始
  19. {
  20. STemp = SFiles[sfs];
  21. STemp = STemp.Substring(0, STemp.Length - 4);//去掉文件名称后缀
  22. SFPath = LPath STemp @"\" STemp;//将要移动的文件夹
  23. ToPath = NewFileCrePath @"\" STemp;//目标文件夹
  24. if (Directory.Exists(SFPath))
  25. {
  26. Directory.Move(SFPath, ToPath);//ToPath要设置为移动到这里的那个文件夹的名字(或者重命名
  27. SFPath = LPath STemp;
  28. Directory.Delete(SFPath, true);
  29. }
  30. else
  31. {
  32. MessageBox.Show("要移动的文件不存在!");
  33. return;
  34. }
  35. //=============================版本生成
  36. FromVerPath = NewFileCrePath @"\" STemp @"\Version.txt";
  37. FileStream fsver = new FileStream(FromVerPath, FileMode.Open);//吧每一个文件的版本提取
  38. StreamReader reader = new StreamReader(fsver, UnicodeEncoding.GetEncoding("GB2312"));//中文、读取
  39. String[] str = new String[5];
  40. for (int i = 0; i < 2; i )
  41. {
  42. str[i] = reader.ReadLine();//赋值
  43. }
  44. StreamWriter sw = new StreamWriter(VerPath, true, UnicodeEncoding.GetEncoding("GB2312"));//写入
  45. str[0] = STemp " " "\t " str[0];
  46. sw.WriteLine(str[0]);//在后面写入
  47. sw.Close();
  48. fsver.Close();
  49. }//for循环结束
  50. CountFile = 0;
  51. string newzip = NewFileCrePath ".zip";
  52. ZipDirectory(NewFileCrePath, newzip);//压缩一下
  53. // FileUpLoad(newzip,"");上传一下
  54. }
  55. else
  56. {
  57. MessageBox.Show("要创建的文件夹已经存在!");
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. MessageBox.Show("error!");
  63. throw ex;
  64. }
  65. }

c语言如何将文件数据删除(c创建删除)(1)

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页