简体   繁体   English

修改Setting.cs文件(Settings.settings的局部类)是否安全?

[英]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? 从标题中可以看出,我修改了Settings.cs文件,向承包商(public Settings())添加了一些属性和代码,并重写了Save()函数,但这是一个patial类,所以我知道您会产生什么样的后果修改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, "");
}

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). 如果您添加的内容不在某个代码生成工具创建的单独文件中,则IDE不会覆盖您的更改(以防万一,请不要将其命名为* .designer.cs结尾)。
So that would be at least safe from the IDE's code generation. 因此,至少从IDE的代码生成中是安全的。

Do not edit the files generated by visual studio (Those file usually have a comment at the top warning you about this). 不要编辑由Visual Studio生成的文件(这些文件通常都不得不警告你这个评论)。

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 注意:尽管如此,该连接字符串中的任何敏感数据仍不安全

to know is it 100% safe or not? 知道它是否100%安全吗?

100% unsafe. 100%不安全。 Designer change = file thrown away. 设计师更改=文件被丢弃。

Make another file, partial, add your code there. 制作另一个文件,部分,在其中添加代码。 ANY CHANGE TO CODE IN THIS FILE IS NOT SAFE AND WILL BE LOST. 对此文件中的代码进行的任何更改都不安全,并且将丢失。

Accident waiting to happen. 等待事故发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM