This C# example code will help you to check if a file exist in a given directory. For example in below code it is looking for test.txt file in test directory. C# provide inbuilt method File.Exists, which return true if file found.
using System;
using System.IO;
namespace MyApp
{
class file_not_found_class
{
public static void Main(string[] my_args)
{
string my_file = @"c:\test\test.txt";
if (File.Exists(my_file))
{
Console.WriteLine(my_file + " found.");
}
else
{
Console.WriteLine("Unable to find file: " + my_file);
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment