TomFarmer32
TomFarmer32

Reputation: 19

How to use IF(File.Exists) statement

I’m currently trying to use an if statement to read to see if a txt file in a folder named Accounts and determine whether that file exists, if it does then it opens up the next window, and if it doesn’t, then the user gets a response saying account does not exist! Here is the code I have

String FilePath = UsernameTextBox.Text;

if(File.Exists(FilePath) 
{
 Welcome openForm = new Welcome();
 openForm.Show();
}
Else
{
MessageBox.Show(“Account Does Not Exist”)
}

It kind of works meaning that if i put the full path into the text box with the extension .txt it opens the next window but I want to be able to just put the FileName in eg HarrySmith and for it to read and open the new window! Thanks in advance

Upvotes: 1

Views: 485

Answers (3)

Hanno Ottens
Hanno Ottens

Reputation: 61

If you want to just enter the file name without extension you could do something like this:

String FilePath = UsernameTextBox.Text + ".txt";

However, if you want both to work you can do this:

String FilePath = UsernameTextBox.Text;
if(Path.GetExtension(FilePath) != ".txt")
{
     FilePath += ".txt";
}

This makes it so when someone does enter the filename with '.txt' on the end it won't add '.txt' to the end twice.

Upvotes: 2

user5900485
user5900485

Reputation:

If you put only the file name without the full path, then the program has no way of knowing where this file is, so it will look only to the current directory where the program is. That being said, if the file extension and the place that these files are saved is constant(i.e it does not change), one possible solution is this:

String FilePath = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData), UsernameTextBox.Text + ".txt");

if(File.Exists(FilePath) 
{
  Welcome openForm = new Welcome();
  openForm.Show();
}
else
{
  MessageBox.Show("Account Does Not Exist")
}

This code will search for a file in the AppData folder named with whatever you entered in the textbox, and the extension ".txt"; that being said, with this solution the only thing you need to do, is to enter the file name in the text box without the full path or the extension. Of course you can change this line of code:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

to whatever path you have saved your files in order to meet your needs.

Upvotes: 1

DxTx
DxTx

Reputation: 3347

If you just want to enter the file name, you have to manually give the file location and file extension. To do that, you can simply do something like this.

string filePath = "C:\\Accounts\\";
filePath += textBoxUsername.Text + ".txt";

if (File.Exists(filePath))
{
   Welcome openForm = new Welcome();
   openForm.Show();
}
else
{ 
   MessageBox.Show(“Account Does Not Exist”)
}

Recommended reading: https://www.dotnetperls.com/file-exists

However, If you are trying to authenticate users, I do not recommend this. I have included some of the StackOverflow questions that might help you accomplish what you need.

Recommended readings
How to properly store password locally
How to securely save username/password (local)?
Retrieve Credentials from Windows Credentials Store using C#
How do I store and retrieve credentials from the Windows Vault credential manager?

Upvotes: 0

Related Questions