Reputation: 3
string[] usernames;
string[] password ;
bool username;
bool passok;
bool match;
string passinp;
string userinp;
protected void Button1_Click(object sender, EventArgs e)
{
StreamReader SrUs = new StreamReader("Usernames.txt");
StreamReader SrPass = new StreamReader("Password.txt");
int i = 0;
while(!SrUs.EndOfStream)
{
usernames[i] = SrUs.ReadLine();
}
i = 0;
while(SrPass.EndOfStream)
{
password[i] = SrPass.ReadLine();
}
if (txt_pass.Text != "" && txt_username.Text != "")
{
passinp = txt_pass.Text;
userinp = txt_username.Text;
}
else
{
Label3.Text = "Missing information";
}
int j = 0;
for(i = 0;i < usernames.Length;i++)
{
if(usernames[i] == userinp)
{
username = true;
break;
}
}
for(j = 0 ; j < password.Length;j++)
{
if(password[j]==passinp)
{
passok = true;
break;
}
}
if(username && passok && i == j)
{
Label3.Text = "Logon Correct";
}
else
{
Label3.Text = "Logon Incorect";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
}
So when I run this it gives me an error that it can't find txt file, is it same in asp.net and in windows form app use of streamreader, I put txt files inside bin and root folder but again it gives me the same error.
Upvotes: 0
Views: 785
Reputation: 306
In ASP.NET you can't use system directories directly. So you have to use Server.MapPath("~/")
to get the Root Directory Path of your application.
You can use like this
string FilePath = Server.MapPath("~/") + "Usernames.txt";
StreamReader SrUs = new StreamReader(FilePath);
Upvotes: 2
Reputation: 65
Your problem is you have no idea where the current DLLs are actually being executed. You have guessed the "bin" directory.
Environment.CurrentDirectory is probably your best bet https://msdn.microsoft.com/en-us/library/system.environment.currentdirectory(v=vs.110).aspx
or see this question on httpcontexts How do I get the current directory in a web service
TBH storing usernames and passwords in text files inside a website working directory is probably a bad idea. Consider a fixed directory store outside of C:\inetpub\ or use ADFS or a database
DO NOT HOMEBAKE YOUR OWN SECURITY CODE
Upvotes: 1