C# 打开带有错误消息的变量文件

C# open variable file with error message

我不知道如何让控制台错误检查用户输入然后打开请求的文件。有人可以告诉我我做错了什么吗?

这是我当前的节目。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication5
{
class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine(">Enter File to open.");//Prompt user for file name

            try
            {
                if (!File.Exists(Console.ReadLine())) 
                throw new FileNotFoundException();//Check for errors

            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("You stuffed up!"); //Display error message
            }
        }
            System.Diagnostics.Process.Start(@Console.ReadLine()); //set valid reply response
            Console.ReadLine();
        }


    }
}

你在这一行中有一个分号 if (!File.Exists(Console.ReadLine())) ;

你不要在 if 语句上加分号,如果你的 if 语句后只有一行,下面的就可以了

if (!File.Exists(Console.ReadLine()))
                throw new FileNotFoundException();//Check for errors

其他

if (!File.Exists(Console.ReadLine())){
                throw new FileNotFoundException();//Check for errors

//some more code
}

编辑:

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine(">Enter File to open.");//Prompt user for file name
            string s = Console.ReadLine();
            try
            {
                if (!File.Exists(s))
                    throw new FileNotFoundException();//Check for errors
                else
                    System.Diagnostics.Process.Start(s); //set valid reply response

                Console.ReadLine();
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("You stuffed up!"); //Display error message
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ABCD
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine(">Enter File to open.");//Prompt user for file name
                string fileName = Console.ReadLine();
                try
                {
                    if (!File.Exists(fileName))
                        throw new FileNotFoundException();//Check for errors
                    else
                        System.Diagnostics.Process.Start(fileName); //set valid reply response

                    Console.ReadLine();
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("You stuffed up!"); //Display error message
                }
            }
        }
    }
}

只需确保输入文件名,即扩展名为 ".exe" 的可执行文件,并确保提供完整路径。

即输入 "C:\Program Files\Internet Explorer\iexplore.exe",打开 Internet Explorer