使用c#中的streamwriter在文本文件中的特定行中写入

时间:2016-08-24 16:42:37

标签: c# streamwriter

我想更新文本文件中的一行。文本文件内容只是数字,并用新行分隔。每一行都分配给一个值。我需要在每次更新值时更新每一行。要知道我应该更新哪一行,我使用if和else if语句来分隔每行的更新。我能够写上它但在写入文本文件后,其中的内容将被删除。我试着为streamwriter和streamreader移动我的声明,但是text文件只能被访问一次。这是我的代码。请帮帮我

try
{
    StreamReader sr = new StreamReader(DEFINES.DISPENSER_STORAGE);
    List<string> lines = new List<string>();
    while (!sr.EndOfStream)
        lines.Add(sr.ReadLine());
    Console.WriteLine("fifty" + lines[0]);
    Console.WriteLine("twenty" + lines[1]);
    Console.WriteLine("ten" + lines[2]);
    Console.WriteLine("five" + lines[3]);
    Console.WriteLine("one" + lines[4]);
    sr.Close();
    StreamWriter sw = new StreamWriter(DEFINES.DISPENSER_STORAGE);
    if (flag == DEFINES.UPDATE_FIFTY)
    {
        string fifty_left = (Convert.ToString(Convert.ToInt32(
              cash_update_local.Get(DEFINES.FIFTY_BILL_COUNT)) -
              Convert.ToInt32(cash_dispenser.Get(
              DEFINES.num_of_fifty_to_dipense))));
        lines[0] = fifty_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("fifty" + lines[0]);
        sw.Close();
    }
    else if (flag == DEFINES.UPDATE_TWENTY)
    {

        string twenty_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.TWENTY_BILL_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_twenty_to_dipense))));
        lines[1] = twenty_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("twenty" + lines[1]);
        sw.Close();
    }
    else if (flag == DEFINES.UPDATE_TEN)
    {
        string ten_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.TEN_COIN_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_ten_to_dipense))));
        lines[2] = ten_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("ten" + lines[2]);
        sw.Close();

    }
    else if (flag == DEFINES.UPDATE_FIVE)
    {

        string five_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.FIVE_COIN_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_five_to_dipense))));
        lines[3] = five_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("five" + lines[3]);
        sw.Close();
    }
    else if (flag == DEFINES.UPDATE_ONE)
    {
        string one_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.ONE_COIN_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_one_to_dipense))));
        lines[4] = one_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("one" + lines[4]);
        sw.Close();
    }
    return return_status;
}

0 个答案:

没有答案