Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将程序输出保存到(.txt)文件_C# - Fatal编程技术网

C# 将程序输出保存到(.txt)文件

C# 将程序输出保存到(.txt)文件,c#,C#,我的节目 namespace trim2 { class Program { static void Main(string[] args) { //ask user to start, if yes then continue, else then close DataConversion() } public static void DataConve

我的节目

namespace trim2
  {
     class Program
      {
        static void Main(string[] args)
          {
            //ask user to start, if yes then continue, else then close

            DataConversion()
          }

    public static void DataConversion()
    {
        string lines = File.ReadAllText(@"d:\trim1.txt");
        string result;
        result = lines.Replace("- - ", string.Empty).Replace("+", string.Empty).....

         \\process goes here
         \\process goes here
         \\process goes here
    }
  }
}
我所期望的是,在文件经过数据转换过程后,它将保存到一个新的文本文件(即已处理的文件)中。。我怎样才能做到这一点

还有,我试过这句话,似乎行不通

 File.WriteAllText("C:\Users\Cleaned.txt", new string(ShiftLine));

\
字符用于指定

由反斜杠(\)后跟字母或数字组合组成的字符组合称为“转义序列”。若要在字符常量中表示换行符、单引号或某些其他字符,必须使用转义序列。转义序列被视为单个字符,因此作为字符常量有效

要避免对字符串进行这种解释,您需要将其写入两次:

File.WriteAllText("C:\\Users\\Cleaned.txt", new string(ShiftLine));
或者使用
@
运算符告诉编译器按字面解释字符串:

File.WriteAllText(@"C:\Users\Cleaned.txt", new string(ShiftLine));
@
是一个

逐字字符串文字由@字符后跟双引号字符、零个或多个字符以及结束双引号字符组成。一个简单的例子是@“你好”。在逐字字符串文字中,分隔符之间的字符被逐字解释


不要忘记
@
@“C:\Users\Cleaned.txt”
您的程序中出现了什么错误?我在代码中的任何地方都没有看到
ShiftLine
,它是如何初始化的?char[]ShiftLine=new char[0];我删除了@MongZhuok的代码,只要你确定所需的数据在
ShiftLine
中,看起来还可以,但为了思想的完整性和可追溯性,发布这部分代码也不错,明白了。非常感谢你。顺便问一下,除了使用这些代码,还有其他方法吗?@AliffM。请详细说明“任何其他方法”。写入文件?您的数据到底是什么样子的?期望的结果是什么?我的意思是,有没有其他方法/代码可以在我的程序中直接保存覆盖的文本文件,这些文本文件具有与此移位线方法类似的功能?@AliffM。是的,有不同的方法。看一看。如果这不是你需要的,请给我进一步的评论