Добавление имени файла сохраненного файла в файл, который будет сохранен - PullRequest
1 голос
/ 11 мая 2011

Я пытаюсь добавить имя файла в первой строке моего текстового файла, который будет сохранен.как мне это сделать?вот кодЯ не смог найти метод для строителя строк, добавляемый в начале его экземпляра.

StringBuilder sb = new StringBuilder();

sb.AppendLine("BLAH BLAH");

if (saveFile.ShowDialog() == DialogResult.OK)
{
   //How to append file name at the beggining of the file to be saved?

    File.WriteAllText(saveFile.FileName, sb.ToString());
}

Спасибо.

Ответы [ 2 ]

2 голосов
/ 11 мая 2011

Добавьте эту строку перед вызовом WriteAllText:

sb.Insert(0, saveFile.FileName + Environment.NewLine);

- или -

string outString = saveFile.FileName + Environment.NewLine + sb.ToString();  
File.WriteAllText(saveFile.FileName, outString);
1 голос
/ 11 мая 2011

Сохранить предыдущий текст:

StringBuilder sb = new StringBuilder();

// Appending string to your StringBuilder string value.
sb.AppendLine("BLAH BLAH");

if (saveFile.ShowDialog() == DialogResult.OK)
{
    // Keep the previous file text .. By inserting it in the begining of the 
    // StringBuilder string value. 
    sb.Insert(0, File.ReadAllText(saveFile.FileName) + Environment.NewLine);

    // Insert File Name in the begining of the StringBuilder string value.
    sb.Insert(0, saveFile.FileName + Environment.NewLine);

    File.WriteAllText(saveFile.FileName, sb.ToString());
}
...