【问题标题】:How to append Text at the begining of a file?如何在文件开头附加文本?
【发布时间】:2015-09-13 14:22:55
【问题描述】:

我正在使用命令IO.File.AppendAllLines("3.txt", "text", System.Text.Encoding.Default) 写信给3.txt。 但是字符串正在写下来。 我如何写一个字符串?

【问题讨论】:

  • 请编辑您的问题以包括所有相关的源代码、它产生的输出示例以及您希望它产生的输出示例。

标签: vb.net string file text


【解决方案1】:

调用字符串的ToUpper方法使字符串变为大写

IO.File.AppendAllLines("3.txt", "text".ToUpper(), System.Text.Encoding.Default)

【讨论】:

    【解决方案2】:

    .Append() 方法将始终将给定的字符串附加(添加)到现有内容。所以在这种情况下你不能这样做。

    我建议您先阅读内容,然后将内容连同附加字符串一起写回文件。如果您对此有所期待,那么以下代码将对您有所帮助:

    var currentContent= File.ReadAllText(@"D:\3.txt");
    File.WriteAllText(@"D:\3.txt", "Some string" + currentContent);
    // here you are adding "Some string" at the begining of the file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2018-05-06
      • 2021-10-22
      • 1970-01-01
      • 2012-03-20
      • 2021-05-30
      相关资源
      最近更新 更多