如何替换文本文件中的文本 [英] How to replace text in a textfile

查看:109
本文介绍了如何替换文本文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序有多个表单,它在第二个表单上创建文本框和标签。此表单上的文本框是密码,我需要保存它,所以当第二个表单加载它时检索密码,如果他们愿意,他们可以改变它



i试图将它写入这样的文件

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

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

我想用一个用户输入的未知值替换tb.text。

我该怎么做?



我尝试了什么:



i已经尝试了

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

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

推荐答案

你想做什么?

您创建一个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();
    }


这篇关于如何替换文本文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆