//为报告设定路径
        Console.WriteLine("输入您要保存报告文档的文档名与路径(例如C:\\文件名.TXT)");
        string strwj = Console.ReadLine();
        Console.WriteLine();
       
        //ping count 次
        string count;
        Console.WriteLine("输入您要ping的次数");
        count = Console.ReadLine();
        Console.WriteLine();
        int m = int.Parse(count);
        for (int i = 0; i < m; i++)
        {
            PingReply reply = ping.Send(ip, 1000, data, options);            if (reply != null)
            {
                switch (reply.Status)
                {
                    case IPStatus.Success:
                        String str = reply.RoundtripTime.ToString();
                        int n = int.Parse(str);
                        if (n > 50)
                        {
                            Console.WriteLine(DateTime.Now.ToString());
                            Console.WriteLine("响应超时");
                            System.IO.FileStream aFile;
                            
                            string fileName = strwj;                            if (!File.Exists("e:\\报告2.txt"))
                            {
                                aFile = new System.IO.FileStream(fileName, System.IO.FileMode.CreateNew);
                            }
                            else
                            {
                                aFile = new System.IO.FileStream(fileName, System.IO.FileMode.Append);
                            }                            StreamWriter sw = new StreamWriter(aFile);                            string text = " ";                            text = DateTime.Now.ToString() + "    响应超时 " + Environment.NewLine;                            sw.Write(text);
                            sw.Close();
                            aFile.Close();
                        }
                        Console.WriteLine("Reply from {0}: " +
                            "bytes={1} time={2}ms TTL={3}",
                            reply.Address, reply.Buffer.Length,
                            reply.RoundtripTime, reply.Options.Ttl);
                        received++;
                        responseTimes.Add(reply.RoundtripTime);
                        break;
                    case IPStatus.TimedOut:
                        System.IO.FileStream bFile;
                        string bfileName = "e:\\报告2.txt";                        if (!File.Exists("e:\\报告2.txt"))
                        {
                            bFile = new System.IO.FileStream(bfileName, System.IO.FileMode.CreateNew);
                        }
                        else
                        {
                            bFile = new System.IO.FileStream(bfileName, System.IO.FileMode.Append);
                        }
                        StreamWriter bw = new StreamWriter(bFile);                        string btext = " ";                        btext = DateTime.Now.ToString() + "   Request timed out " + Environment.NewLine;                        bw.Write(btext);
                        bw.Close();
                        bFile.Close();
1:怎么用通过控制台输入得到的路径代替我原来静态的路径?
2:我现在每一种情况访问文件都要通过上面标红处的设定,每次都要设定新的字符串变量,十分麻烦,如何修改?
贡献全部积分!

解决方案 »

  1.   


    internal class Program {
            private static void Main(string[] args) {
                //...
                string strwj = Console.ReadLine(); 
                //...
                //...
                string content = string.Empty;
                // 真正需要保存的内容放到content里面,然后
                SaveFile(strwj,content);        }        private static void SaveFile(string filePath, string content) {
                using(StreamWriter sw = new StreamWriter(filePath,true)) {
                    sw.WriteLine("{0}:{1}\n", DateTime.Now, content);
                    sw.Flush();
                    sw.Close();
                }
            }
        }