static void Main(string[] args)
        {
            string myFile = Process.GetCurrentProcess().MainModule.FileName;
            string Cmd = "@echo off\n:try   \ndel " + myFile + "\nif exist " + myFile + " goto   try\ndel %0";
            StreamWriter myStreamWriter = new StreamWriter("suicide.bat",true,Encoding.Default);
            myStreamWriter.Write(Cmd);           
            myStreamWriter.Close();//批处理里"\n"乱码了,根本没换行
            Console.WriteLine(Cmd); //显示正常
            Console.ReadLine();
        }
总不要:myStreamWriter.WriteLine();一行行来吧?

解决方案 »

  1.   

    myStreamWriter.Write(Cmd.Replace("\n",\r\n"));
      

  2.   

    string Cmd = "@echo off\n:try  \ndel " + myFile + "\nif exist " + myFile + " goto  try\ndel %0"; 
    改成:
    string Cmd = "@echo off\\n:try  \\ndel " + myFile + "\\nif exist " + myFile + " goto  try\\ndel %0"; 或者:
    string Cmd = @"@echo off\n:try  \ndel " + myFile + "\nif exist " + myFile + " goto  try\ndel %0"; 
      

  3.   

    string Cmd = "@echo off\n:try  \ndel " + myFile + "\nif exist " + myFile + " goto  try\ndel %0"; 你这行中的\n已经被C#转义了. 应该把所有的 \n 变成 \\n
      

  4.   

    同志们误会了,可能是我表达不好.我要的是"\n"是换行作用,不是把"\n"输入到文件.
    这样还是乱码.
    2,3,4楼结果是:
    @echo off\n:try   \ndel C:\Documents and Settings\不能说的\My Documents\Visual Studio 2005\Projects\自杀程序\自杀程序\bin\Debug\自杀程序.vshost.exe
    if exist C:\Documents and Settings\不能说的\My Documents\Visual Studio 2005\Projects\自杀程序\自杀程序\bin\Debug\自杀程序.vshost.exe goto   try
    del %0
    这样的批处理有什么用呢?
    我要的是\n就换行!!!!!!!!!!!!!!!!!!!!!!!!@echo off
    :try
    del C:\Documents and Settings\不能说的\My Documents\Visual Studio 2005\Pro
    jects\自杀程序\自杀程序\bin\Debug\自杀程序.vshost.exe
    if exist C:\Documents and Settings\不能说的\My Documents\Visual Studio 200
    5\Projects\自杀程序\自杀程序\bin\Debug\自杀程序.vshost.exe goto   try
    del %0
      

  5.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.IO;namespace 自杀程序
    {
        class Program
        {
            static void Main(string[] args)
            {
                string myFile = Process.GetCurrentProcess().MainModule.FileName;
                string Cmd = "@echo off\\n:try   \\ndel " + myFile + "\\nif exist " + myFile + " goto   try\\ndel %0";
                StreamWriter myStreamWriter = new StreamWriter("suicide.bat",false,Encoding.Default);
                //false若已有则覆盖没有则创建(true若已有同名文件存在回添加到其内容后面)
                myStreamWriter.Write(Cmd);
                myStreamWriter.Close();
                Console.WriteLine(Cmd);
                Console.ReadLine();
            }
        }
    }
    同志们要实验啊,自己试试看看结果是什么.
      

  6.   

    string Cmd = "@echo off\\n :try   \\n del " + myFile + "\\n if exist " + myFile + " goto   try\\n del %0";
    请加上空格,谢谢
      

  7.   

                string myFile = Process.GetCurrentProcess().MainModule.FileName;
                string Cmd = "@echo off\n:try  \ndel " + myFile + "\nif exist " + myFile + " goto  try\ndel %0";
                string[] cmds = Cmd.Split( "\n".ToCharArray() );
                StreamWriter myStreamWriter = new StreamWriter( "suicide.bat", true, Encoding.Default );
                foreach( string cmd in cmds )
                {
                    myStreamWriter.WriteLine( Cmd );
                }
                
                myStreamWriter.Close();//批处理里"\n"乱码了,根本没换行
                Console.WriteLine( Cmd ); //显示正常
                Console.ReadLine(); 
      

  8.   

    你加上空格看看行不行?根本不行!!!!!!!!11楼的.我就是想饶过
    myStreamWriter.WriteLine("@echo off);
    myStreamWriter.WriteLine(try);
    myStreamWriter.WriteLine(*********);
    myStreamWriter.WriteLine(*******);
    这种格式.
      

  9.   

    为什么不用WriteLine?这个很好啊
      

  10.   

    就是一个将文本保存到文档中换行的问题,  string Cmd = "@echo off" + Environment.NewLine + ":try " + Environment.NewLine + "del " + myFile + Environment.NewLine + "if exist " + myFile + " goto  try" + Environment.NewLine + "del %0";
    OK
      

  11.   

    string Cmd = "@echo off" +"\r\n"+ ":try " +"\r\n"+  "del " + myFile+"\r\n"+  "if exist " + myFile + " goto  try" +"\r\n"+  "del %0"; 
    也可以
      

  12.   

                string myFile = Process.GetCurrentProcess().MainModule.FileName;
                string Cmd = "@echo off\n:try  \ndel " + myFile + "\nif exist " + myFile + " goto  try\ndel %0";
                Cmd = Cmd.Replace( @"\n", Environment.NewLine );
                StreamWriter myStreamWriter = new StreamWriter( "suicide.bat", true, Encoding.Default );
                myStreamWriter.WriteLine( Cmd );
                myStreamWriter.Close();//批处理里"\n"乱码了,根本没换行
                Console.WriteLine( Cmd ); //显示正常
                Console.ReadLine();
    不过我就是用你的代码运行结果竟然是对的。
      

  13.   

    string myFile = Process.GetCurrentProcess().MainModule.FileName; 
                string Cmd = @"@echo off\n:try  \ndel " + myFile + "\nif exist " + myFile + " goto  try\ndel %0"; 
                Cmd = Cmd.Replace( @"\n", Environment.NewLine ); 
                StreamWriter myStreamWriter = new StreamWriter( "suicide.bat", true, Encoding.Default ); 
                myStreamWriter.WriteLine( Cmd ); 
                myStreamWriter.Close();
                Console.WriteLine( Cmd );
                Console.ReadLine();