写入单个文本文件时冲突的文件流

时间:2014-02-28 15:55:44

标签: c# filestream

我有三个按钮标题/正文/页脚.MY标题在行尾有空格1983年然后我的身体开始于1984年,当我首先保存我的身体,同时导出到文本文件它复制很好,但当我点击标题附加文件的一半数据被标题替换为空格。我可以先保存我的身体,然后在附加标题时向下推动身体是可能的。

我的标题:

FileStream fs = new FileStream(@"C:\Users\IT-Administrator\Desktop\ee.txt", FileMode.Open, FileAccess.ReadWrite);
fs.Seek(0, SeekOrigin.Begin);

StreamWriter sw = new StreamWriter(fs);
//sw.WriteLine(comboBox7.Text +comboBox2.Text +textBox6.Text);
sw.WriteLine("{0}{1}{2}{3,-1983}", comboBox7.Text, comboBox2.Text,textBox6.Text, ' ');
sw.Close();
fs.Close();

我的身体按钮:

if (myDataset == null)
{
    return;
}

if (myDataset.Tables[0].Rows.Count == 0)
{
    return;
}

DataView vwExport = new DataView(myDataset.Tables[0]);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "TXT file|*.txt";
sfd.FileName = "ticket_info "  + ".txt";

if (sfd.ShowDialog() == DialogResult.OK)
{
    if (sfd.FileName != "")
    {
        ExportDatatviewToCsv(sfd.FileName, vwExport);
        MessageBox.Show("File has been saved as: " + Environment.NewLine + sfd.FileName + Environment.NewLine + Environment.NewLine + "NB: This dataset has been ordered by t_reference in ascending order. If being combined with an existing dataset - that dataset will also need to be sorted in this way.", "Operation complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

1 个答案:

答案 0 :(得分:0)

简单的答案是:

  1. 将正文写入文件并保存;
  2. 将标题写入临时文件(最好是在内存中);
  3. 读取包含正文的文件内容(在readwrite mdoe中打开)将其添加到tempfile;
  4. 使用内存流中的联接数据覆盖文件中正文的内容。
  5. 这是预先添加到文件的最简单方法。 我希望这会对你有所帮助。

    此致,Jordy