Click here to Skip to main content
15,887,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that has more than one form and it creates the textboxes and labels on the second form.the textbox on this form is the "password" and i need to save it so when the second form loads it retrieves the password and makes it so they can change it if they wish

i tried to write it to the file like this
C#
private void buttonclick(object sender,EventArgs e)
{
                counter += 1;
                TextBox tb = new TextBox();
                Label labl = new Label();
                Label num = new Label();
                tb.Text = textbox.Text;
                labl.Text ="hello";
                num.Text = counter.ToString();
                string currentEntry = num.Text + "|" + tb.Text.Replace("|", "~") + "|" 
                                 + labl.Text + "|";
                File.AppendAllText("savedpass.txt", currentEntry);
                this.Hide();
}
I want to replace a tb.text with some unknown value entered by a user.
How do i do this ??

What I have tried:

i have tried
C#
using (StreamReader sr = new StreamReader("savedpass.txt"))
                {
                    string line = "";
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] lineData = line.Split('|');
                        string num = lineData[1];
                        ReplaceInFile("savedpass.txt",textbox.text,num);
                        
                    }
                    sr.Close();
                }
but it comes up with the error file is being used by another process
Posted
Updated 28-Jul-18 10:17am
v5

1 solution

What are you trying to do?
You create a TextBox - which you never display - set its Text property from a different textbox, create two labels you never display, and then hide everything to be on the safe side...

Try this:
private void buttonclick(object sender,EventArgs e)
    {
    string currentEntry = string.Format("{0}|{1}|{2}", 
                                        ++counter, 
                                        textBox.Text.Replace("|", "~"), 
                                        "hello");
    File.AppendAllText("savedpass.txt", currentEntry);
    this.Hide();
    }

As for your "What I have tried" code, of course you get a "file in use" error - you are probably using the file! Either there is a mistype in the file names and you actually have "savedpass.txt" in both places, or your ReplaceInFile method is not releasing the file properly.
using (StreamReader sr = new StreamReader("savedpass.txt"))
    {
    string line = "";
    while ((line = sr.ReadLine()) != null)
        {
        string[] lineData = line.Split('|');
        string num = lineData[1];
        ReplaceInFile("savepass.txt",textbox.text,num);
        
        }
    sr.Close();
    }
 
Share this answer
 
Comments
Codingnow20 28-Jul-18 16:12pm    
@OriginalGriff i updated my question sorry for the misunderstanding but i will try your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900