1

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

  • 5
    Files names are not unique on modern operating systems. You'll have to tell the OS where to look (the folder). – Mixxiphoid Apr 16 '18 at 09:27
  • If you only put the filename in how will it know what directory to look in? If you want to just hardcode that then just use that and the filename and `Path.Combine` to create your actual path to use. – Chris Apr 16 '18 at 09:28
  • 3
    You should consider to use a more reliable user-authentication system ;-) – Tim Schmelter Apr 16 '18 at 09:28
  • You can add a logic of selecting the file, keep the full path to the file in the variable and update the value in the shown field to the file name – Samvel Petrosov Apr 16 '18 at 09:28
  • Sorry I’m completely new to this! I’m not sure, How would I specify just looking for the file name, as if. I put the path into the text box when searching it allows it to work? – TomFarmer32 Apr 16 '18 at 09:29
  • Yeah I know using this type of user authentication is that efficient at all, but I’m just playing around with it – TomFarmer32 Apr 16 '18 at 09:30
  • It's not really that it's not _efficient_ (although that may be true), more that it's not very _secure_. – ADyson Apr 16 '18 at 09:34
  • Yeah I’m just playing around with it at the moment! Just trying to get use to everything – TomFarmer32 Apr 16 '18 at 09:35

3 Answers3

2

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.

Hanno Ottens
  • 41
  • 11
1

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.

0

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?

DxTx
  • 2,469
  • 2
  • 15
  • 28