gpt4 book ai didi

c# - 检查用户是否输入了正确的文件系统路径

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:14 46 4
gpt4 key购买 nike

我正在创建一个小的 C# 程序,它从“用户选择”的位置读取纺织品。我已经设法正确读取文件,但如果用户键入文件名/路径不正确....或者文件类型不正确。我已经在我有限的知识范围内尝试了一切,现在我有点卡住了。任何帮助都将不胜感激。谢谢

using System;

class ReadFromFile
{
static void Main()
{
Console.WriteLine ("Welcome to Decrypter (Press any key to begin)");
Console.ReadKey ();

//User selects file they wish to decrypt
int counter = 0;
string line;
string path;


Console.WriteLine ("\nPlease type the path to your file");
path = Console.ReadLine ();

// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader (path);

while ((line = file.ReadLine ()) != null) {

Console.WriteLine (line);
counter++;
}

file.Close ();

// Suspend the screen.
Console.ReadLine ();
}
}

最佳答案

使用

try
{
if (!File.Exists(path))
{
// Tell the user
}
}
catch (Exception ex)
{
// tell the user
}

文件存在

添加

using System.IO;

到代码文件顶部

关于c# - 检查用户是否输入了正确的文件系统路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438046/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com