无法捕获用户非输入

时间:2019-09-27 20:04:26

标签: c# .net

我想抓住用户输入无效的时间以及他们没有输入的时间。我只能捕获无效的输入。

string filename;
            Bitmap picture;
            Console.Write("Write the file you want to edit; ")
            try
            {
                filename = Console.ReadLine();
                picture = new Bitmap(filename);
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("No file choosen");
                Console.ReadLine();
                return;
            }
            catch (ArgumentException)
            {
                Console.WriteLine("File not valid");
                Console.ReadLine();
                return;

1 个答案:

答案 0 :(得分:1)

我将使用IsNullOrEmpty(),例如:

if (string.IsNullOrEmpty(filename)
{
    // throw an error
}
// otherwise, continue