internal static class UnsafeNativeMethods
{
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
}
public class FileExtensionRegistrator
{
public const string fileExtension = ".bla";
public const string keyName = "bla_bla_file";
public const string fileDescription = "It is my BLA file";
public static void SetAssociation(string openWith)
{
SetAssociation(fileExtension, keyName, openWith, fileDescription);
}
public static void SetAssociation(string extension, string keyName, string openWith, string fileDescription)
{
RegistryKey baseKey;
RegistryKey openMethod;
RegistryKey shell;
RegistryKey currentUser;
baseKey = Registry.ClassesRoot.CreateSubKey(extension);
baseKey.SetValue("", keyName);
openMethod = Registry.ClassesRoot.CreateSubKey(keyName);
openMethod.SetValue("", fileDescription);
openMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + openWith + "\",0");
shell = openMethod.CreateSubKey("Shell");
shell.CreateSubKey("open").CreateSubKey("command").SetValue("", string.Format("\"{0}\" -cmd=open -file=\"%1\"",openWith));
baseKey.Close();
openMethod.Close();
shell.Close();
// Delete the key instead of trying to change it
currentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true);
if (currentUser != null)
{
currentUser.DeleteSubKey("UserChoice", false);
currentUser.Close();
}
// Tell explorer the file association has been changed
UnsafeNativeMethods.SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
public static void RemoveAssociation()
{
RemoveAssociation(fileExtension, keyName);
}
public static void RemoveAssociation(string extension, string keyName)
{
try
{
Registry.ClassesRoot.DeleteSubKeyTree(extension);
Registry.ClassesRoot.DeleteSubKeyTree(keyName);
}
catch {}
// Tell explorer the file association has been changed
UnsafeNativeMethods.SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
}
FileExtensionRegistrator.SetAssociation(Application.ExecutablePath);
public TimeSpan TimeOfDay { get; }
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace XmlSerializeDemo
{
[XmlRoot("TechTree")]
public class TechCollection
{
[XmlAttribute("version")]
public int Version { get; set; }
[XmlElement("Tech")]
public TechItem Item { get; set; }
}
public class TechItem
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
var e = new TechItem() { Type = "Noraml", Name = "Age 1" };
var t = new TechCollection() { Version = 5, Item = e };
Serialize(@".\demo.xml", t);
}
private static void Serialize<T>(string path, T obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Document;
settings.OmitXmlDeclaration = false; // не подавлять xml заголовок
settings.Encoding = Encoding.UTF8; // кодировка
settings.Indent = true; // добавлять отступы
settings.IndentChars = " "; // сиволы отступа
// подавляем неймспейсы
XmlSerializerNamespaces dummyNSs = new XmlSerializerNamespaces();
dummyNSs.Add(string.Empty, string.Empty);
using (FileStream fileStream = new FileStream(path, FileMode.Create))
{
using (XmlWriter xw = XmlWriter.Create(fileStream, settings))
{
serializer.Serialize(xw, obj, dummyNSs);
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<TechTree version="5">
<Tech type="Noraml" name="Age 1" />
</TechTree>
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace XmlSerializeDemo
{
[XmlRoot("TechTree")]
public class Tech
{
[XmlAttribute("version")]
public int Version { get; set; }
[XmlElement("Info")]
public string Description { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
var t = new Tech() { Version = 5, Description = "I'm xml" };
Serialize(@".\demo.xml", t);
}
private static void Serialize<T>(string path, T obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Document;
settings.OmitXmlDeclaration = false; // не подавлять xml заголовок
settings.Encoding = Encoding.UTF8; // кодировка
settings.Indent = true; // добавлять отступы
settings.IndentChars = " "; // сиволы отступа
// подавляем неймспейсы
XmlSerializerNamespaces dummyNSs = new XmlSerializerNamespaces();
dummyNSs.Add(string.Empty, string.Empty);
using (FileStream fileStream = new FileStream(path, FileMode.Create))
{
using (XmlWriter xw = XmlWriter.Create(fileStream, settings))
{
serializer.Serialize(xw, obj, dummyNSs);
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<TechTree version="5">
<Info>I'm xml</Info>
</TechTree>