Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

visual studio - how would i make a chain of if statements more efficient? C#

I did see this one asked yet (if it did I might have missed it), but I have a chain of if statements that are here to lead the computer into a user-specified location on my local pc and perform a specific action when there.

static void Main(string[] args)
        {
            string ChosenGame;
            string ListofGames = "Minecraft, 7 Days to Die, ...";
            string MVOM;
            string MMod;
            var SourProjectOzone3 = @"C:UsersleonTwitchMinecraftInstancesProject Ozone 3 A New Way Forward";
            var ProjectOzone3Temp = string.Format("{0} on {1:ddd MM.dd.yyy 'At' HH.mm tt}", @"F:BackupsMinecraftModdedProject Ozone 3Project Ozone 3", DateTime.Now);
            var ProjectOzone3BU = string.Format("{0} on {1:ddd MM.dd.yyy 'At' HH.mm tt}", @"\ds1817MainBackupsMinecraftModdedProject Ozone 3Project Ozone 3", DateTime.Now);
            var Save = @"C:UsersleonAppDataRoaming7DaysToDieSavesBigboy 2 NonePrivate 16K A19";
            var soursdtd = @"C:UsersleonAppDataRoaming7DaysToDieSaves";
            var tempsdtd = string.Format("{0} on {1:ddd MM.dd.yyy 'At' HH.mm tt}", @"F:Backups7 Days to Die7 Days to Die", DateTime.Now);
            var sdtdBU = string.Format("{0} on {1:ddd MM.dd.yyy 'At' HH.mm tt}", @"\ds1817MainBackups7 Days to Die7 Days to Die", DateTime.Now);
            Console.WriteLine(ListofGames);
            Console.WriteLine("What Game do you want to back up?");
            ChosenGame = Console.ReadLine();
            if (ChosenGame.IndexOf("minecraft", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                Console.WriteLine("Vanilla or Modded?");
                MVOM = Console.ReadLine();
                if (MVOM.IndexOf("modded", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    List<string> FolderName = new List<string>();
                    DirectoryInfo di = new DirectoryInfo("C:\Users\leon\Twitch\Minecraft\Instances");
                    DirectoryInfo[] diArr = di.GetDirectories();
                    foreach (DirectoryInfo dri in diArr)
                    {
                        FolderName.Add(dri.Name);
                    }
                    Console.WriteLine("The Modpacks are " + string.Join(", ", FolderName));
                    Console.WriteLine("What Modpack?");
                    MMod = Console.ReadLine();
                    if (MMod.IndexOf("project ozone 3", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        while (Directory.Exists(SourProjectOzone3)){
                            Console.WriteLine("Backup beginning...");
                            //Now Create all of the directories
                            foreach (string dirPath in Directory.GetDirectories(SourProjectOzone3, "*", SearchOption.AllDirectories))
                                Directory.CreateDirectory(dirPath.Replace(SourProjectOzone3, ProjectOzone3Temp));

                            //Copy all the files
                            foreach (string newPath in Directory.GetFiles(SourProjectOzone3, "*.*", SearchOption.AllDirectories))
                                File.Copy(newPath, newPath.Replace(SourProjectOzone3, ProjectOzone3Temp));
                            Console.WriteLine("I'm Done with 1/2!");

                            //Now Create all of the directories
                            foreach (string dirPath1 in Directory.GetDirectories(ProjectOzone3Temp, "*", SearchOption.AllDirectories))
                                Directory.CreateDirectory(dirPath1.Replace(ProjectOzone3Temp, ProjectOzone3BU));

                            //Copy all the files
                            foreach (string newPath1 in Directory.GetFiles(ProjectOzone3Temp, "*.*", SearchOption.AllDirectories))
                                File.Copy(newPath1, newPath1.Replace(ProjectOzone3Temp, ProjectOzone3BU));
                            Console.WriteLine("I'm Done with 2/2!");
                            Thread.Sleep(1800000);
                        }
                    }
                }
                if (MVOM.IndexOf("vanilla", StringComparison.OrdinalIgnoreCase) >= 0)
                {

                }
            }

            if (ChosenGame.IndexOf("7 days to die", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                string worldchoice;
                string savechoice;
                List<string> FolderName = new List<string>();
                //Makes a reference to a directory.
                DirectoryInfo di = new DirectoryInfo("C:\Users\leon\AppData\Roaming\7DaysToDie\Saves");
                //Gets a reference to each directory in that directory.
                DirectoryInfo[] diArr = di.GetDirectories();

                //Displays the names of the directories.
                foreach (DirectoryInfo dri in diArr){
                    FolderName.Add(dri.Name);
                }
                Console.WriteLine("The Worlds are " + string.Join(", ", FolderName));
                Console.WriteLine("what World would you like to backup?");
                savechoice = Console.ReadLine();
                if (savechoice.IndexOf("Bigboy 2 None", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    List<string> FolderName1 = new List<string>();
                    //Makes a reference to a directory.
                    DirectoryInfo di1 = new DirectoryInfo("C:\Users\leon\AppData\Roaming\7DaysToDie\Saves\Bigboy 2 None");
                    //Gets a reference to each directory in that directory.
                    DirectoryInfo[] diArr1 = di1.GetDirectories();

                    //Displays the names of the directories.
                    foreach (DirectoryInfo dri1 in diArr1)
                    {
                        FolderName1.Add(dri1.Name);
                    }
                    Console.WriteLine("The Saves are " + string.Join(", ", FolderName1));
                    Console.WriteLine("What Save would you like to backup?");
                    worldchoice = Console.ReadLine();
                    if (worldchoice.IndexOf("Private 16K A19", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        while (Directory.Exists(Save))
                        {
                            Console.WriteLine("Backup beginning...");
                            //Now Create all of the directories
                            foreach (string dirPath1 in Directory.GetDirectories(Save, "*", SearchOption.AllDirectories))
                                Directory.CreateDirectory(dirPath1.Replace(Save, tempsdtd));

                            //Copy all the files
                            foreach (string newPath2 in Directory.GetFiles(Save, "*.*", SearchOption.AllDirectories))
                                File.Copy(newPath2, newPath2.Replace(Save, tempsdtd));
                            Console.WriteLine("I'm Done with 1/2!");

                            //Now Create all of the directories
                            foreach (string dirPath2 in Directory.GetDirectories(tempsdtd, "*", SearchOption.AllDirectories))
                                Directory.CreateDirectory(dirPath2.Replace(tempsdtd, sdtdBU));

                            //Copy all the files
                            foreach (string newPath2 in Directory.GetFiles(tempsdtd, "*.*", SearchOption.AllDirectories))
                                File.Copy(newPath2, newPath2.Replace(tempsdtd, sdtdBU));
                            Console.WriteLine("I'm Done with 2/2!");
                            Thread.Sleep(300000);
                        }
                    }
                }
            }
        }

I have seen some cases where people have used switch statements instead of if statements but that wasn't in a chain of if statements. So I'm just wondering if there is a way to make this more efficient to write and compile but not lose functionality.

question from:https://stackoverflow.com/questions/65838868/how-would-i-make-a-chain-of-if-statements-more-efficient-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...