c# - 使用 .replace 替换文本文档中的单词 (c#)

标签 c# asp.net replace

目前有以下代码:

    string[] fileLineString = File.ReadAllLines(Server.MapPath("~") + "/App_Data/Users.txt");

    for (int i = 0; i < fileLineString.Length; i++)
    {
        string[] userPasswordPair = fileLineString[i].Split(' ');

        if (Session["user"].ToString() == userPasswordPair[0])
        {
            userPasswordPair[i].Replace(userPasswordPair[1], newPasswordTextBox.Text);
        }
    }
}

文本文件设置为:'用户名''密码

我想做的是能够使用我的代码编辑密码并用新密码替换它,但我的代码似乎什么也没做,文本文件只是保持不变。

最佳答案

string[] fileLineString = File.ReadAllLines(Server.MapPath("~") + "/App_Data/Users.txt");

for (int i = 0; i < fileLineString.Length; i++)
{
    string[] userPasswordPair = fileLineString[i].Split(' ');

    if (Session["user"].ToString() == userPasswordPair[0])
    {
        // set the new password in the same list and save the file
        fileLineString[i] = Session["user"].ToString() + " " + newPasswordTextBox.Text;
        File.WriteAllLines((Server.MapPath("~") + "/App_Data/Users.txt"), fileLineString);
        break; // exit from the for loop
    }
}

关于c# - 使用 .replace 替换文本文档中的单词 (c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16882086/

相关文章:

c# - 使用 Masterpage 时无法在内容页面中运行 javascript

html - XSLT - 将 <p> 添加到文本字符串中而不是\n

c# - 如何最好地确定新应用程序的系统要求?

c# - 什么是NullReferenceException,如何解决?

c# - 在 MVC4 中更改运行时的路由

asp.net - Firefox 中出现奇怪的 SignalR 错误,即使我没有使用 SignalR

delphi - 替换文件中的字符(更快的方法)

javascript - .replace javascript 不工作

c# - 将 GIF 文件保存到图片文件夹吗?

c# - ServiceStack 拦截请求/响应以进行验证