This article will show you how you can search a file existence it
presence in a directory or folder in windows
application using c#.net.
Some of my previous articles are as follows: Get
List Of All Files Present in Directory In Windows Application Using C#.Net, Bind
DataGridView With DataTable Using C#.Net In Windows Application, Display
DataGridView Selected Row Values To Second From Using C# In Windows Application, Bind
DataGridView With Sql Server Table Using C#.Net In Windows Application, How
To Convert Text Into Image In C# In Windows Application, Detecting
Ctrl+ V Key Pressed Or Stroke Using C# & VB.Net In Windows Application, Read
Outlook Email Subject Using c#.
So for this article first we will create a new windows
application and add some button and textbox control on form. Your form will
look as shown below:
After this we will create click event of button and add the below code on
button click event.
private void button1_Click(object sender, EventArgs e)
{
string[] filePaths = Directory.GetFiles(txtDirectoryPath.Text);
string filename =
txtFileName.Text;
foreach (string item in filePaths)
{
if (item.Contains(filename))
{
lblmessage.Text = "File
Exists";
break;
}
else
{
lblmessage.Text = "File NOT
Exists";
}
}
}
|
In above code first I have fetched all the files present in the provided
directory and then I have applied look to checked and every file one by one
weather the file is present in the collection or not.
If it’s found then it
will show the message “File Exists” otherwise it will display “File Not Found”.
Now we have done run the application to check the output. First we will
check the file directory.
Now we will provide directory path and file name.
Now we will provide invalid file name.
0 comments:
Please let me know your view