C# 修改Setting.cs文件(Settings.Settings的部分类)安全吗

C# 修改Setting.cs文件(Settings.Settings的部分类)安全吗,c#,app-config,partial,C#,App Config,Partial,正如您从标题中所了解的,我修改了Settings.cs文件。向constractor(public Settings())添加了一些属性和代码,并覆盖了Save()函数。但它是一个特殊的类,所以我知道当修改IDE生成的文件时,尤其是.designers时,您必须面对什么样的后果。它不是设计器,而是IDE生成的内部密封部分,我想知道它是否100%安全 internal sealed partial class Settings { public System.Data.SqlClient.

正如您从标题中所了解的,我修改了Settings.cs文件。向constractor(public Settings())添加了一些属性和代码,并覆盖了Save()函数。但它是一个特殊的类,所以我知道当修改IDE生成的文件时,尤其是.designers时,您必须面对什么样的后果。它不是设计器,而是IDE生成的内部密封部分,我想知道它是否100%安全

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, "");
}

如果您添加的内容位于一个独立的文件中,而该文件不是由某个代码生成工具创建的,那么IDE不会覆盖您的更改(只是为了以防万一,不要将其命名为以*.designer.cs结尾的文件)。
因此,这至少可以避免IDE的代码生成

不要编辑visual studio生成的文件(这些文件的顶部通常会有一条注释,提醒您注意这一点)

这样的自动生成文件是将类声明为部分类的主要原因之一,这样您就可以在另一个文件中扩展它,而不用担心更改被覆盖


注意:该连接字符串中的任何敏感数据都是不安全的,但如果您添加的内容位于一个独立的文件中,而该文件不是由某些代码生成工具创建的,则IDE不会覆盖您的更改(只是为了以防万一,不要将其命名为以*.designer.cs结尾的内容)。
因此,这至少可以避免IDE的代码生成

不要编辑visual studio生成的文件(这些文件的顶部通常会有一条注释,提醒您注意这一点)

这样的自动生成文件是将类声明为部分类的主要原因之一,这样您就可以在另一个文件中扩展它,而不用担心更改被覆盖

注意:该连接字符串中的任何敏感数据都是不安全的

知道它是否100%安全

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, "");
}
100%不安全。设计器更改=文件被丢弃

制作另一个文件,部分,在那里添加代码。对此文件中的代码所做的任何更改都是不安全的,将丢失

事故即将发生

知道它是否100%安全

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, "");
}
100%不安全。设计器更改=文件被丢弃

制作另一个文件,部分,在那里添加代码。对此文件中的代码所做的任何更改都是不安全的,将丢失


意外正在等待发生。

确定如何解决此问题。当您首次创建Settings.cs(双击properties下的Settings.Settings并单击view code)时,一些注释,如://此类允许您在Settings类上处理特定事件;//要添加事件处理程序以保存和更改设置,请取消注释下面的行,//在此处添加代码以处理SettingChangingEvent事件。如果它是100%不安全的,并且我的代码将丢失,为什么VS2010会添加这些注释。@EmrahTOPCU Nice find,甚至没有注意到您可以这样扩展设置。这就像是设置背后的代码。在这种情况下,我认为您可以修改它。非常感谢Botz3000提供的帮助。确定如何完成此操作。当您首次创建Settings.cs(双击properties下的Settings.Settings并单击view code)时,一些注释(如://此类)允许您处理Settings类上的特定事件,//若要添加用于保存和更改设置的事件处理程序,请取消下面几行的注释,//在此处添加用于处理SettingChangingEvent事件的代码。如果它100%不安全,并且我的代码将丢失,为什么VS2010要添加这些注释。@EmrahTOPCU Nice find,甚至没有注意到您可以这样扩展设置。这就像是设置背后的代码。如果是那样的话,我认为你可以修改它。非常感谢你对Botz3000的帮助。