如何更改控制台应用程序以将输出发送到winforms列表框?

时间:2010-05-27 21:20:48

标签: c# winforms

对,我有一个控制台应用程序,它将我需要的数据......输出到控制台!

我想要做的就是,在此数据开始生成之前启动一个Windows窗体,然后将数据打印到控制台,然后添加到表单上的列表框中。

我不希望听起来很懒,但我一直在四处走动,我无处可去。我在Program.cs中有我的控制台代码

有人可以帮忙吗?谢谢

using System;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using EasyHook;

namespace FileMon
{
    public class FileMonInterface : MarshalByRefObject
    {
        public void IsInstalled(Int32 InClientPID)
        {      
            Console.WriteLine("FileMon has been installed in target {0}.\r\n", InClientPID);
        }

        public void OnCreateFile(Int32 InClientPID, String[] InFileNames)
        {
            for (int i = 0; i < InFileNames.Length; i++)
            {
                String[] s = InFileNames[i].ToString().Split('\t');

                if (s[0].ToString().Contains("ROpen"))
                {
                    Console.WriteLine(DateTime.Now.Hour+":"+DateTime.Now.Minute+":"+DateTime.Now.Second+"."+DateTime.Now.Millisecond + "\t" + s[0] + "\t" + getProcessName(int.Parse(s[1])) + "\t" + getRootHive(s[2]));
                }
                else if (s[0].ToString().Contains("RQuery"))
                {
                    Console.WriteLine(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond + "\t" + s[0] + "\t" + getProcessName(int.Parse(s[1])) + "\t" + getRootHive(s[2]));
                }
                else if (s[0].ToString().Contains("RDelete"))
                {
                    Console.WriteLine(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond + "\t" + s[0] + "\t" + getProcessName(int.Parse(s[0])) + "\t" + getRootHive(s[1]));
                }
                else if (s[0].ToString().Contains("FCreate"))
                {
                    //Console.WriteLine(DateTime.Now.Hour+":"+DateTime.Now.Minute+":"+DateTime.Now.Second+"."+DateTime.Now.Millisecond + "\t" + s[0] + "\t" + getProcessName(int.Parse(s[1])) + "\t" + s[2]);
                }
            }
        }

        public void ReportException(Exception InInfo)
        {
            Console.WriteLine("The target process has reported an error:\r\n" + InInfo.ToString());
        }

        public void Ping()
        {
        }

        public String getProcessName(int ID)
        {
            String name = "";
            Process[] process = Process.GetProcesses();
            for (int i = 0; i < process.Length; i++)
            {
                if (process[i].Id == ID)
                {
                    name = process[i].ProcessName;
                }
            }
            return name;
        }

        public String getRootHive(String hKey)
        {
            int r = hKey.CompareTo("2147483648");
            int r1 = hKey.CompareTo("2147483649");
            int r2 = hKey.CompareTo("2147483650");
            int r3 = hKey.CompareTo("2147483651");
            int r4 = hKey.CompareTo("2147483653");

            if (r == 0)
            {
                return "HKEY_CLASSES_ROOT";
            }
            else if (r1 == 0)
            {
                return "HKEY_CURRENT_USER";
            }
            else if (r2 == 0)
            {
                return "HKEY_LOCAL_MACHINE";
            }
            else if (r3 == 0)
            {
                return "HKEY_USERS";
            }
            else if (r4 == 0)
            {
                return "HKEY_CURRENT_CONFIG";
            }
            else return hKey.ToString();
        }
    }

    static class Program 
    {
        static String ChannelName = null;
        static void Main()
        {

            try
            {
                Config.Register("A FileMon like demo application.", "FileMon.exe", "FileMonInject.dll");
                RemoteHooking.IpcCreateServer<FileMonInterface>(ref ChannelName, WellKnownObjectMode.SingleCall);
                Process[] p = Process.GetProcesses();
                for (int i = 0; i < p.Length; i++)
                {
                    try
                    {
                        RemoteHooking.Inject(p[i].Id, "FileMonInject.dll", "FileMonInject.dll", ChannelName);
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            catch (Exception ExtInfo)
            {
                Console.WriteLine("There was an error while connecting to target:\r\n{0}", ExtInfo.ToString());
            }
            Console.ReadLine();

        }
    }
}

3 个答案:

答案 0 :(得分:3)

你需要理解的唯一一件事是,WinForm只是C#中的另一个类...创建一个winform并在你从class.cs调用的类中放一个方法,而不是调用console .writeline

答案 1 :(得分:1)

为什么不制作Windows窗体应用程序?如果你需要在命令行上执行某些操作,你可以创建一个命令行进程......看起来比让控制台应用程序调用表单更合理,但我不明白为什么这样做也不行。

答案 2 :(得分:0)

您使用的是Visual Studio吗?如果您创建一个Forms项目,VS Form Designer将创建一个空白的Form控件;将代码复制到它。

查看MSDN上的Creating a New Forms Application