I have a program where it randomly generates a new password and then writes it on a text file.
The issue is that when I try to generate a new password, it writes the new password plus the previously generated password.
I want to just write down the newly generated password, excluding the previous one
This is the code for the string password:
string password = "";
string pathUserPass = @"C:\Users\Name\Desktop\test\UserPass.txt";
string pathXD = @"C:\Users\Name\Desktop\test";
var fileUserPass = new FileInfo(pathUserPass);
string Response;
int r, k;
Random Rand = new Random();
for (int i = 0; i < 10; i++)
{
r = Rand.Next(3);
if (r == 0)
{
k = Rand.Next(0, 25);
password += charPassword[k];
}
else if (r == 1)
{
k = Rand.Next(0, 25);
password += charPassword[k];
}
else if (r == 2)
{
k = Rand.Next(0, 9);
password += charPassword[k];
}
}
This is my code that writes a newly generated password:
else if (File.Exists(pathUserPass) && fileUserPass.Length > 0)
{
while(true)
{
try
{
using (var tw = new StreamWriter(pathUserPass, true))
{
while (password == password)
{
for (int i = 0; i < 10; i++)
{
r = Rand.Next(3);
if (r == 0)
{
k = Rand.Next(0, 25);
password += charPassword[k];
}
else if (r == 1)
{
k = Rand.Next(0, 25);
password += charPassword[k];
}
else if (r == 2)
{
k = Rand.Next(0, 9);
password += charPassword[k];
}
}
tw.WriteLine(password);
Console.WriteLine("Wrote {0}", password);
}
}
Console.Read();
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e);
Console.Read();
}
catch (IOException e)
{
Console.WriteLine(e);
Console.Read();
}
}
OutPut:
awbicsoatfibutdbqgrf
awbicsoatfibutdbqgrfkabeokksnd
awbicsoatfibutdbqgrfkabeokksndedmmscajpd
awbicsoatfibutdbqgrfkabeokksndedmmscajpdjdcqsepbcg
awbicsoatfibutdbqgrfkabeokksndedmmscajpdjdcqsepbcgyidlctlcpk
awbicsoatfibutdbqgrfkabeokksndedmmscajpdjdcqsepbcgyidlctlcpkiidjfcbiih
awbicsoatfibutdbqgrfkabeokksndedmmscajpdjdcqsepbcgyidlctlcpkiidjfcbiihbccqhuxpgg
Any ideas of taking out the previous generated string? I appreciate anyone reading this. Thx :)
Aucun commentaire:
Enregistrer un commentaire