用线条在文本文件上写下问题

时间:2014-11-10 20:57:20

标签: c# console-application outputstream writer file-writing

代码: 好吧我想用控制台aplication进行配置继承我的代码,我从web重写一些模板问题是输出fille不是我想要的请求帮助:

using System;
using System.IO;
using System.Text;

namespace testwriter
{
    class Program
   {
        static void Main()
        {
            try
            {
                FileStream fs = new FileStream("c:\\test\\test.ini", FileMode.Create);
               StreamWriter writer = new StreamWriter(fs);
                StringBuilder output = new StringBuilder();
                int repeat = 0;

                do
                {
                    writer.WriteLine("[Settings]");
                    writer.WriteLine("Domain=test");
                    writer.WriteLine("Server=test");
                    writer.WriteLine("Proxy=test");
                    writer.WriteLine("SRTP=test");
                    writer.WriteLine("Transport=test");
                    writer.WriteLine("PublicAddr=test");
                    writer.WriteLine("ListenPort=test");
                    writer.WriteLine("DisplayName=test");
                    writer.WriteLine("var=");
                    writer.WriteLine("var1=0");
                    writer.WriteLine("var1=0");
                    writer.WriteLine("DisableLocalAccount=1");
                    writer.WriteLine("notification=ringin2.wav");
                    writer.WriteLine("ActiveTab=0");
                    Console.WriteLine("yourname: ");
                    writer.Write("Username=");
                    output.Append(Console.ReadLine() + "\r\n");
                    Console.WriteLine ("yourpass:" +"\r\n");
                    writer.Write("\r\n" + "Password=");
                    output.Append(Console.ReadLine());
                    writer.WriteLine(output.ToString());
                    output.Clear();
                    Console.WriteLine("Repeat? 1-Yes, 0-No : ");
                    repeat = Convert.ToInt32(Console.ReadLine());
                } while (repeat != 0);

                writer.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

当我编译exe输出时是这个结构:

  • [设置]
    • 域=测试
    • 服务器=测试
    • 代理=测试
    • SRTP =测试
    • 运输=测试
    • PublicAddr =测试
    • 的listenPort =测试
    • DisplayName的=测试
    • VAR = 0
    • VAR1 = 0
    • VAR1 = 0
    • DisableLocalAccount = 1
    • 通知= ringin2.wav
    • ActiveTab = 0
    • 用户名=
    • 密码=测试
    • 测试

如何使用户名和密码成为键入的内容。如何在程序关闭后按任意键运行一些.exe

1 个答案:

答案 0 :(得分:0)

writer.WriteLine方法中输入用户输入:

//for user id
Console.WriteLine("yourname: ");
writer.WriteLine("Username=" + Console.ReadLine());

//for password
Console.WriteLine("yourpass: ");
writer.WriteLine("Password=" + Console.ReadLine());

也无需使用StringBuilder

启动新流程。创建Process的实例:

System.Diagnostics.Process proc = new Process();
proc.StartInfo = new ProcessStartInfo("notepad.exe");
proc.Start();