1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Windows.Forms;
namespace projet
{
class Create_XML
{
private static Create_XML instance;
private String cheminFichier = Launcher.filename +"\\"+"TEST.xml";
private XDocument document;
private Create_XML()
{
string nameFile = IncrementFileName(cheminFichier);
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(nameFile));
try
{
//MessageBox.Show(nameFile);
document = XDocument.Load(nameFile);
}
catch (FileNotFoundException )
{
Initialiser();
}
}
public static Create_XML xml
{
get
{
if (instance == null)
{
instance = new Create_XML();
}
return instance;
}
}
public void Initialiser()
{
string nameFile = IncrementFileName(cheminFichier);
if (document == null)
{
document = new XDocument(
new XElement("test",
new XElement("row",
new XElement("num" , "0"),
new XElement("name","caro"),
new XElement("min","1"),
new XElement("max","10")
)));
System.Console.WriteLine(document);
document.Save(nameFile);
}
}
/*******Incrémentation du nom du fichier XML**********/
public string IncrementFileName(string filename)
{
if (!File.Exists(filename))
return filename;
string path = System.IO.Path.GetDirectoryName(filename);
string ext = System.IO.Path.GetExtension(filename);
string name = System.IO.Path.GetFileName(filename).Substring(0, System.IO.Path.GetFileName(filename).Length - ext.Length);
int count = 0;
string newFilename;
do
{
count++;
newFilename = String.Format("{0}({1}){2}",System.IO.Path.Combine(path,name), count,ext);
}while(File.Exists(newFilename));
return newFilename;
}
}
} |
Partager