且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何替换文本文件中的文本

更新时间:2022-04-02 22:38:24

你想做什么?

您创建一个TextBox - 您永远不会显示 - 从不同的文本框设置其Text属性,创建两个您永远不会显示的标签,然后将所有内容隐藏起来以保证安全... >


试试这个:

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();
    }



至于你的我试过的代码,当然你得到一个正在使用的文件错误 - 你可能正在使用该文件!文件名中有错误类型,并且实际上两个地方都有savedpass.txt,或者您的ReplaceInFile方法没有正确释放文件。


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();
    }