开发者

Example for Replace file user menu

I have an application that writes files from a directory. This software can read only 1 file or several of them, example:

Case 1:  "file_1.dat"
Case 2: "file_1.dat", "file_2.dat", "file_3.dat"...etc

I make this, shows this message if the file exists in a directory. The problem that I have is if the user write "N", and the program needs continues reading other files, I don't know how continues asking the same question and not cancel the process if the user write "N". I need the program continues the same process normally checking if the others files exists. So how can I do?

if (File.Exists(binaryFilePath))
            {
                Program.DisplayUserOptionMessage("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N");
                //string overwrite = Console.ReadLine();
                 while (true)
                 {
                    string overwrite = Console.ReadLine();
                    if (overwrite.ToUpper() == "Y")
                    {
                       WriteBinaryFile(frameCodes, binaryFilePath);
                       break;
                    } 
                    else if (overwrite.ToUpper() == "N")
                    {

                        //Program.DisplayExceptionMessage("apply this for all the rest?? ");
                        throw ne开发者_开发问答w IOException();                        
                    } 
                    else
                    {
                    Program.DisplayUserOptionMessage("!!Please Select a Valid Option!!");
                    overwrite = Console.ReadLine();
                    continue;
                    } 
                }
            }


It looks like the code snippet you have is called by the loop that is processing the files. I'd imagine the IOException you're throwing would kick you out of that loop... not sure why you need to do that, since it's not really an exception?

Your best bet is to catch this exception in the loop that is processing the files, handle it (write to a log, or continue), and continue with the next item in the loop.

Other option is to simply 'return' instead of throwing an IOException.

I think you have one other small glitch, if the user enters an invalid value, you will prompt him twice for a valid one. Drop the overwrite = Console.ReadLine(); in the else{...} loop... you're making that call at the beginning of the next loop.


I don't think you need that while (true) loop. Rather while (!isFileProcessed) (more on that in a moment). I assume you're looping over input file names, if so then it is fine to ask "The file exist. You want to overwrite it? Y/N" question for every file.

However, your program probably needs to be structured differently:

string[] filesNames = new[] { "file_1.dat", "file_2.dat", "file_3.dat" };
foreach (var name in filesNames)
{  
    // ... 

    if (binaryExists)
    {
        bool isFileProcessed = false;
        Program.DisplayUserOptionMessage("The file: " + binaryFileName 
            + " exist. You want to overwrite it? Y/N");
        while (!isFileProcessed)
        {
            string overwrite = Console.ReadLine();
            if (overwrite.ToUpper() == "Y")
            {
                WriteBinaryFile(frameCodes, binaryFilePath);
                isFileProcessed = true; 
            }
            else if (overwrite.ToUpper() == "N")
            {
                isFileProcessed = true; 
            } 
            else
            {
                // here we do nothing with isFileProcessed flag: 
                // user still needs to select proper option, loop continues 
                Program.DisplayUserOptionMessage(
                    "!!Please Select a Valid Option!!");
            }
        } 
     }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜