通过变量减少“ if”的数量

时间:2018-06-28 00:31:43

标签: c# .net winforms

正确if除以变量

代码很快就会变成这个(会有很多if值)

if
 if
  if
   if
    if
     if
      if
       if
       else
      else
     else
    else
   else
  else
 else
else

...

    private static void Main(string[] args)
    {
        bool onlyInstance;
        Mutex mtx = new Mutex(true, "AppName", out onlyInstance);
        if (onlyInstance)
        {
            var whitelist = new HashSet<string> { ".txt" };
            if (args.Any() && whitelist.Contains(Path.GetExtension(args[0])))
            {
                args = Environment.GetCommandLineArgs();
                if (args.Length > 1)
                {
                    string filePath = args[1];
                    string fileName = Path.GetFileName(filePath);
                    string fileNameNoExt = Path.GetFileNameWithoutExtension(filePath);
                    string fileExt = Path.GetExtension(filePath);
                    string fileFolderDir = Path.GetDirectoryName(filePath);
                    string newFileName = String.Format("{0}.my1", fileName);
                    string createFolderIn = Environment.ExpandEnvironmentVariables("%AppData%\\AppName\\Copy\\");
                    string dstPath = Environment.ExpandEnvironmentVariables("%AppData%\\AppName\\Copy\\") + newFileName;
                    string systemFolderLS = Environment.ExpandEnvironmentVariables("%SystemRoot%\\System32");
                    string systemFolderLs = Environment.ExpandEnvironmentVariables("%SystemRoot%\\system32");
                    Directory.CreateDirectory(createFolderIn);
                    File.Copy(filePath, dstPath, true);
                    if ((fileFolderDir == systemFolderLS) || (fileFolderDir == systemFolderLs))
                    {
                        Process ProcAll = new Process();
                        ProcAll.StartInfo.Verb = "runas";
                        ProcAll.StartInfo.FileName = fileNameNoExt + ".exe";
                        ProcAll.Start();
                        Application.Exit();
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new Form1());
                    }
                }
                else
                {
                    Application.Exit();
                }
            }
            else
            {
                Application.Exit();
            }
        }
        else
        {
            Application.Exit();
        }
    }

1 个答案:

答案 0 :(得分:3)

您可以做的一件事是减少嵌套。

免责声明:这完全基于观点。

Mutex mtx = new Mutex(true, "AppName", out bool onlyInstance);

if (!onlyInstance)
   return;

var whitelist = new HashSet<string> { ".txt" };

if (!args.Any() || !whitelist.Contains(Path.GetExtension(args[0])))
   return;

args = Environment.GetCommandLineArgs();

// also this is redundant 
//if (args.Length <= 1)
//   return;

var filePath = args[1];
var fileName = Path.GetFileName(filePath);
var fileNameNoExt = Path.GetFileNameWithoutExtension(filePath);
var fileExt = Path.GetExtension(filePath); // this is never used
var fileFolderDir = Path.GetDirectoryName(filePath);
var newFileName = $"{fileName}.my1";
var createFolderIn = Environment.ExpandEnvironmentVariables("%AppData%\\AppName\\Copy\\");
var dstPath = Environment.ExpandEnvironmentVariables("%AppData%\\AppName\\Copy\\") + newFileName;

// not sure why you have 2 of these but its ugly and looks redundent
var systemFolderLS = Environment.ExpandEnvironmentVariables("%SystemRoot%\\System32"); 
var systemFolderLs = Environment.ExpandEnvironmentVariables("%SystemRoot%\\system32");
Directory.CreateDirectory(createFolderIn);
File.Copy(filePath, dstPath, true);

if (fileFolderDir == systemFolderLS || fileFolderDir == systemFolderLs)
{
   var ProcAll = new Process();
   ProcAll.StartInfo.Verb = "runas";
   ProcAll.StartInfo.FileName = fileNameNoExt + ".exe";
   return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

免责声明2 :完全未经测试,因此我不对您致残的人或对此代码造成伤害的人负责