Search code examples
c#app-configpartial

Is it safe to modify Setting.cs file (partial class of Settings.settings)


As you can understand from title, i modified Settings.cs file.Added some properties, some code to constractor (public Settings()) and overrided Save() function.But it is a patial class so i know what kind of consequences you have to face when you modify IDE generated files especially .designers.It is not a designer but an IDE generated internal sealed partial and i want to know is it 100% safe or not?

internal sealed partial class Settings
{
    public System.Data.SqlClient.SqlConnection AppDbConnection
    {
      get 
      {
        if (_AppDbConnection == null)
        {
          try
          {
            _AppDbConnection = new System.Data.SqlClient.SqlConnection(_ConnectionString);
            _AppDbConnection.Open();
          }
          catch (System.Exception ex) { throw ex; }
        }
        return _AppDbConnection;
      }
    }

    private System.Data.SqlClient.SqlConnection _AppDbConnection;
    private string _ConnectionString;


public override void Save()
{
  System.IO.FileInfo fi = new System.IO.FileInfo(System.Windows.Forms.Application.StartupPath + "\\Settings.dat");
  System.IO.FileStream fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
  System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding("iso-8859-9"));
  try { sw.Write(Helper.BinarySerializer.ToBinary(_SettingsBase)); }
  catch (System.Exception ex) { throw ex; }
  finally { sw.Close(); fs.Close(); }
  base.Save();
}


    public Settings()
    {
      try
      {
        System.IO.FileInfo fi = new System.IO.FileInfo(System.Windows.Forms.Application.StartupPath + "\\Settings.dat");
        if (fi.Exists)
        {
          System.IO.FileStream fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
          System.IO.StreamReader sr = new System.IO.StreamReader(fs, System.Text.Encoding.GetEncoding("iso-8859-9"));
          string data = sr.ReadToEnd();
          if (data != "")
          {
_SettingsBase = (AppCode.SettingsBase)Helper.BinarySerializer.BinaryTo(data);
            _ConnectionString = Helper.Crypto.DecryptString(_SettingsBase.ConnectionString, "");
}

Solution

  • If your additions are in a separate file that was not created by some code generation tool, then the IDE won't overwrite your changes (just don't name it something ending with *.designer.cs, just in case).
    So that would be at least safe from the IDE's code generation.

    Do not edit the files generated by visual studio (Those file usually have a comment at the top warning you about this).

    Autogenerated files like this are one of the main reasons to declare classes as partial, so that you can extend it in another file without fear that your changes are overwritten.

    Note: Any sensitive data in that connection string won't be safe though