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 to save with unique ID

How to save with unique ID

Scheduled Pinned Locked Moved C#
helpc++xmltutorialcareer
4 Posts 4 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.
  • M Offline
    M Offline
    manju 123
    wrote on last edited by
    #1

    Hi all.. I am trying to write to a xml file... The problem i am facing is....I am writing in a for loop.. Each time it is replacing with the old one.. I am trying with the code..

    for (int indx = 0; indx < contact.Fields.Count; indx++)
    {

                  StreamWriter sw = new StreamWriter(@"D:\\Temp\\File.xml");
                  sw.WriteLine(contact.Xml);
                  sw.Flush();
                  sw.Close();
    

    }

    what i wanted is change the file name with File1.xml,File2.xml,....etc can anyone help.. thank you manju

    Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

    L realJSOPR 2 Replies Last reply
    0
    • M manju 123

      Hi all.. I am trying to write to a xml file... The problem i am facing is....I am writing in a for loop.. Each time it is replacing with the old one.. I am trying with the code..

      for (int indx = 0; indx < contact.Fields.Count; indx++)
      {

                    StreamWriter sw = new StreamWriter(@"D:\\Temp\\File.xml");
                    sw.WriteLine(contact.Xml);
                    sw.Flush();
                    sw.Close();
      

      }

      what i wanted is change the file name with File1.xml,File2.xml,....etc can anyone help.. thank you manju

      Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Depends on whether you want to overwrite the files written from an older for loop. If this is the case, then you can just write the filename as @"D:\Temp\File.xml"+i If you don't want to overwrite files at all, then you'd first have to examine all filenames in the directory (via Directory.GetFiles()) and get the highest file id ever assigned, and start the counter from there. regards

      H 1 Reply Last reply
      0
      • L Lost User

        Depends on whether you want to overwrite the files written from an older for loop. If this is the case, then you can just write the filename as @"D:\Temp\File.xml"+i If you don't want to overwrite files at all, then you'd first have to examine all filenames in the directory (via Directory.GetFiles()) and get the highest file id ever assigned, and start the counter from there. regards

        H Offline
        H Offline
        HosamAly
        wrote on last edited by
        #3

        @"D:\Temp\File" + i + ".xml" is probably more useful.

        Know me better

        1 Reply Last reply
        0
        • M manju 123

          Hi all.. I am trying to write to a xml file... The problem i am facing is....I am writing in a for loop.. Each time it is replacing with the old one.. I am trying with the code..

          for (int indx = 0; indx < contact.Fields.Count; indx++)
          {

                        StreamWriter sw = new StreamWriter(@"D:\\Temp\\File.xml");
                        sw.WriteLine(contact.Xml);
                        sw.Flush();
                        sw.Close();
          

          }

          what i wanted is change the file name with File1.xml,File2.xml,....etc can anyone help.. thank you manju

          Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          Use a GUID for a filename - they're guaranteed to be unique, and therefore aren't susceptible to stopping/restarting the program.

          for (int indx = 0; indx < contact.Fields.Count; indx++)
          {
          string guid = Guid.NewGuid().ToString().Replace("-","").Replace("{","").Replace("}","");
          StreamWriter sw = new StreamWriter(string.Format("D:\\Temp\\{0}.xml", guid));
          sw.WriteLine(contact.Xml);
          sw.Flush();
          sw.Close();
          }

          If you don't want to delete the hyphens or curly braces, remove the Replace() method calls.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          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