Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I clear a file's content?

How can I clear a file's content?

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kidult
    wrote on last edited by
    #1
            if (File.Exists(paraFile))
                File.Delete(paraFile);
            using (FileStream fs = new FileStream(paraFile, FileMode.Create))
            {
                //fs.Seek(0, SeekOrigin.Begin);
                //fs.SetLength(0);
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    //声明数据流文件写入方法
                    sw.WriteLine(markLineOfRobotReason);
                    sw.WriteLine("");
                    foreach (string reason in robotRegisterReasons)
                        sw.WriteLine(reason);
                    sw.WriteLine("");
                    sw.WriteLine(markLineOfHumanReason);
                    sw.WriteLine("");
                    foreach (string reason in humanRegisterReasons)
                        sw.WriteLine(reason);
                    test++;
                    sw.WriteLine("");
                    sw.Write("第" + test + "次写末尾参数");
                    sw.WriteLine(markLineOfExecRate);
                    sw.WriteLine("");
                    sw.WriteLine(comboBox1.SelectedItem.ToString());
                    sw.WriteLine("");
                    sw.WriteLine(markLineOfIfShown);
                    sw.WriteLine("");
                    sw.WriteLine(checkBox1.Checked);
                }
            }
    

    result is like this:

    Quote:

    第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第6次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True

    why?I think it should only 1 times...

    W 1 Reply Last reply
    0
    • K kidult
              if (File.Exists(paraFile))
                  File.Delete(paraFile);
              using (FileStream fs = new FileStream(paraFile, FileMode.Create))
              {
                  //fs.Seek(0, SeekOrigin.Begin);
                  //fs.SetLength(0);
                  using (StreamWriter sw = new StreamWriter(fs))
                  {
                      //声明数据流文件写入方法
                      sw.WriteLine(markLineOfRobotReason);
                      sw.WriteLine("");
                      foreach (string reason in robotRegisterReasons)
                          sw.WriteLine(reason);
                      sw.WriteLine("");
                      sw.WriteLine(markLineOfHumanReason);
                      sw.WriteLine("");
                      foreach (string reason in humanRegisterReasons)
                          sw.WriteLine(reason);
                      test++;
                      sw.WriteLine("");
                      sw.Write("第" + test + "次写末尾参数");
                      sw.WriteLine(markLineOfExecRate);
                      sw.WriteLine("");
                      sw.WriteLine(comboBox1.SelectedItem.ToString());
                      sw.WriteLine("");
                      sw.WriteLine(markLineOfIfShown);
                      sw.WriteLine("");
                      sw.WriteLine(checkBox1.Checked);
                  }
              }
      

      result is like this:

      Quote:

      第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第6次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True 第3次写末尾参数------------运行频率(小时):------------ 1 ------------是否显示作业窗口:------------ True

      why?I think it should only 1 times...

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem. Based on current info, perahps you're wondering why

      foreach (string reason in robotRegisterReasons)
      sw.WriteLine(reason);

      prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.

      K 2 Replies Last reply
      0
      • W Wendelius

        The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem. Based on current info, perahps you're wondering why

        foreach (string reason in robotRegisterReasons)
        sw.WriteLine(reason);

        prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.

        K Offline
        K Offline
        kidult
        wrote on last edited by
        #3

        You got it,you are right,I made a mistake...

        1 Reply Last reply
        0
        • W Wendelius

          The question is unclear. Try to rephrase it and give all the necessary information to investigate the problem. Based on current info, perahps you're wondering why

          foreach (string reason in robotRegisterReasons)
          sw.WriteLine(reason);

          prints many times but that would happen robotRegisterReasons array (or list or what ever it is) contains multiple items.

          K Offline
          K Offline
          kidult
          wrote on last edited by
          #4

          You got it,you are right,I made a mistake...Thank you after all.

          W 1 Reply Last reply
          0
          • K kidult

            You got it,you are right,I made a mistake...Thank you after all.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            Glad if it helped :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups