Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a task in that i get a mdf file, and i have to attach programatically that file to database and when another file comes i have to detach that and take backup of that and attach new one. My problem is, by taking backup i want to give names of that file in incremental order like backup1, backup2.....and so on. I need help how can i give names in incremental order programatically.
Posted

File.Move[^]
For taking the backup (i.e. copying to a new name)
File.Copy[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 14:34pm    
Sure, a 5.
--SA
Use Directory Class[^] and Directory.EnumerateFiles[^] to find all files in the folder with "backup*".

Use LINQ to sort and get the MAX file count name. Like
C#
var file = (from file in Directory.EnumerateFiles(@"c:\archives1\library\", "backup*.txt")
            order by file
            select file).LastOrDefault();


And then use File class[^] file operation.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 14:34pm    
Sure, a 5.
--SA
Kim Togo 16-Jun-11 14:39pm    
Thanks SA
I would keep an int variable in the Application Settings, which I would use and increment every time I needed to backup. Something like this (where Properties.Settings.Default.fileCount is the variable stored in Application Settings

C#
int currentFileNumber = Properties.Settings.Default.fileCount;
           string newFileName = String.Format("YourFileName{0}.mdb", currentFileNumber + 1);
           File.Copy(string.Format("YourFileName{0}.mdb", currentFileNumber), string.Format("YourFileName{0}.mdb", currentFileNumber + 1));
           Properties.Settings.Default.fileCount = currentFileNumber + 1;
           Properties.Settings.Default.Save();


Hope this helps
 
Share this answer
 
If want to do that then u can do that by global variable or if u want it to exist after re-executing the application then u can do it by keeping value in xml file

C#
DataSet ds = new DataSet();

ds.ReadXml("XMLFile1.xml");
int dfs = int.Parse(ds.Tables[0].Rows[0]["value"].ToString());

// Create a new XmlTextWriter instance
XmlTextWriter writer = new XmlTextWriter("XMLFile1.xml", System.Text.Encoding.UTF8);

// start writing!
writer.WriteStartDocument();
writer.WriteStartElement("Visit");
writer.WriteStartElement("Count");
writer.WriteElementString("value", (dfs + 1).ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();


Here is the XML
XML
<?xml version="1.0" encoding="UTF-8"?>
<Visit>
  <Count>
    <text>Visitor</text>
    <value>222</value>
  </Count>
</Visit>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900