с# Serialization to file or string / с# Сериализация в файл или в строку + Десериализация

static void SerializeToFile(object o)
{
  XmlSerializer serializer = new XmlSerializer(o.GetType());
  using (Stream writer = new FileStream("output.xml", FileMode.Create))
  {
    serializer.Serialize(writer, o);
  }
}

public static string SerializeToString(object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());

            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);

                return writer.ToString();
            }
        }
public static object DeserializeFromFile(object obj,string path)
{
  XmlSerializer serializer = new XmlSerializer(obj.GetType());
 
  using (XmlReader reader = XmlReader.Create(path))
  {
    object o = serializer.Deserialize(reader);
  }
}

//From string

var serializer = new XmlSerializer(typeof(Car));
using (var reader = new StringReader(xml))
{
    var car = (Car)serializer.Deserialize(reader);
}

Изменить строку подключения в app.conig / How to change connection string in app.config

static void AddConnectionStringMyDB()
        {
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);
            ConnectionStringsSection csSection =
              config.ConnectionStrings;
            ConnectionStringSettings connection = new ConnectionStringSettings();
            connection.Name = "Rozn_Client.Properties.Settings.userDBConnectionString";
            connection.ProviderName = "Microsoft.SqlServerCe.Client.4.0";
            connection.ConnectionString = "Data Source=|DataDirectory|\\myDB.sdf;Password=mypassword;Persist Security Info=True";
            csSection.ConnectionStrings.Add(connection);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");
        }

C# Get the version of your solution in Visual Studio

string s1 = Application.ProductVersion; //Only product version
string s2 = System.Reflection.Assembly.GetExecutingAssembly().FullName; // Returns Project1, Version=1.0.2281.26155, Culture=neutral, PubliKeyToken=null

c# получить из сертификата открытый и закрытый ключ / Get from X509 certificate private and public keys

X509Certificate2 cert = new X509Certificate2("cert.pfx");
//(RSACryptoServiceProvider)cert.PrivateKey
//(RSACryptoServiceProvider)cert.PublicKey.Key

c# проверяем подпись сертификата x509 у файла / c# Verify x509 certificate signature

public static bool CertificateSignVerify(string signFilePath, byte[] data, RSACryptoServiceProvider rsaKey)
        {
            byte[] signature = File.ReadAllBytes(signFilePath);
            return rsaKey.VerifyData(data, new SHA1CryptoServiceProvider(), signature);
        }

c# How to encrypt file using x509 certificate

public static void EncryptFile(string encrDumpFilePath, string dumpFilePath, RSACryptoServiceProvider rsaKey)
        {
            using (AesManaged aesManaged = new AesManaged())
            {
                aesManaged.KeySize = 256;
                aesManaged.BlockSize = 128;
                aesManaged.Mode = CipherMode.CBC;
                using (ICryptoTransform transform = aesManaged.CreateEncryptor())
                {
                    RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(rsaKey);
                    byte[] keyEncrypted = keyFormatter.CreateKeyExchange(aesManaged.Key, aesManaged.GetType());
                    byte[] LenK = new byte[4];
                    byte[] LenIV = new byte[4];
                    int lKey = keyEncrypted.Length;
                    LenK = BitConverter.GetBytes(lKey);
                    int lIV = aesManaged.IV.Length;
                    LenIV = BitConverter.GetBytes(lIV);
                    int startFileName = dumpFilePath.LastIndexOf("\\") + 1;

                    string outFile = encrDumpFilePath;
                    Directory.CreateDirectory(Path.GetDirectoryName(encrDumpFilePath));
                    using (FileStream outFs = new FileStream(outFile, FileMode.Create))
                    {
                        outFs.Write(LenK, 0, 4);
                        outFs.Write(LenIV, 0, 4);
                        outFs.Write(keyEncrypted, 0, lKey);
                        outFs.Write(aesManaged.IV, 0, lIV);
                        using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
                        {
                            int count = 0;
                            int offset = 0;
                            int blockSizeBytes = aesManaged.BlockSize / 8;
                            byte[] data = new byte[blockSizeBytes];
                            int bytesRead = 0;

                            using (FileStream inFs = new FileStream(dumpFilePath, FileMode.Open))
                            {
                                do
                                {
                                    count = inFs.Read(data, 0, blockSizeBytes);
                                    offset += count;
                                    outStreamEncrypted.Write(data, 0, count);
                                    bytesRead += blockSizeBytes;
                                }
                                while (count > 0);
                                inFs.Close();
                            }
                            outStreamEncrypted.FlushFinalBlock();
                            outStreamEncrypted.Close();
                        }
                        outFs.Close();
                    }
                }
            }
        }