简体   繁体   中英

String.Replace( ) erasing the whole string- C#

I have a piece of code where I am modifying the contents of a file. I actually need to replace a line in the file with a new line. For this I am doing this:

    private void btn_edit_Click(object sender, EventArgs e)
    {
        bufferedListView1.Items.Clear();
        StreamReader sr1 = new StreamReader("C:\\sample.txt");
        string file= sr1.ReadToEnd();
        if (file.Contains(pname + "@" + pno))
        {
            file.Replace(pname + "@" + pno, txt_editname.Text+"@"+txt_editno.Text);//Results null in file
        }
        string efile= sr1.ReadToEnd(); // returns null
        sr1.Close();
        StreamWriter sw1 = new StreamWriter("C:\\sample.txt");
        sw1.Write(efile);
        sw1.Close();
        //Rest of the code

pname, pno contains old values. txt_editname,txt_editno contains new values

I end up having no content in the file sample.txt . What is the reason?

No, your call to file.Replace is doing absolutely nothing useful - you're not using the return value.

Strings are immutable in .NET, so methods like Replace don't change the existing string, they create a new one and return a reference to it. You want:

file = file.Replace(pname + "@" + pno, ...);

And as that's not going to do anything when the search string isn't in the text, you can do that unconditionally.

The next problem is that you're doing this:

string file= sr1.ReadToEnd();
... // code which doesn't change sr1 ...
string efile= sr1.ReadToEnd(); // returns null

That's not actually returning null - it's returning an empty string... because you're still reading from the same StreamReader that you've already read to the end of. Why are you doing that?

Note that you're not even using the file variable after you've called Replace .

Additionally, your code is missing using statements, so if an exception is thrown you'll be leaking file handles (until a finalizer cleans them up). You can avoid all of that very easily though - I suspect this will do what you want:

private void btn_edit_Click(object sender, EventArgs e)
{
    bufferedListView1.Items.Clear();
    string fileContents = File.ReadAllText("C:\\sample.txt");
    string replacedContents = fileContenxt.Replace(pname + "@" + pno, 
        txt_editname.Text + "@" + txt_editno.Text);
    File.WriteAllText("C:\\sample.txt", replacedContents);
    // Rest of code
}

Also note that if this is in a WPF or WinForms app, you really shouldn't be doing all this IO in a UI thread...

file.Replace(pname + "@" + pno, txt_editname.Text+"@"+txt_editno.Text);//Results null in file 

返回一个字符串,您必须再次将其分配给文件。

file = file.Replace(pname + "@" + pno, txt_editname.Text+"@"+txt_editno.Text);//Results null in file

normal, you do this

string efile= sr1.ReadToEnd(); // returns null
...
sw1.Write(efile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM