如何从其他应用程序获取值并将其写入文件?

时间:2015-03-12 09:22:08

标签: c#

我正在尝试创建一个没有UI的简单应用程序,它将从另一个应用程序获取数据并使用C#将其存储在输出文件中。就这样。我怎么做:
1.打开流程
2.找到值“X”
 2.1找到值“Y”
 2.2找到值“Z”
3.将值写入文件“C:\ output.txt”
4.例如每1000ms更新一次。

这是我难以运行的两个代码。
从应用程序中读取:

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

public class MemoryRead
{
    const int PROCESS_WM_READ = 0x0010;

    [DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

    [DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(int hProcess, 
      int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

    public static void Main()
    {
        Process process = Process.GetProcessesByName("notepad")[0]; 
        IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id); 

        int bytesRead = 0;
        byte[] buffer = new byte[24];    

        ReadProcessMemory((int)processHandle, 0x0001BA5B0, buffer, buffer.Length, ref bytesRead);

        Console.WriteLine(Encoding.Unicode.GetString(buffer) + 
           " (" + bytesRead.ToString() + "bytes)");
        Console.ReadLine();
    }

}

写入文件:

class WriteTextFile
{
    static void Main()
    {
        string[] lines = { "X", "Y", "Z", };
        System.IO.File.WriteAllLines(@"O:\CS\output.txt", lines);

    }
}

2个问题:
 1.如何“连接”这两个代码
 2.如何每1秒/ 2秒/ 300毫秒使程序刷新输出?

感谢您的帮助! 附:如果您知道任何现有的应用程序将执行完全相同的操作 - 那对我来说就没问题了。

更新
最后做了两件一起工作!

    using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

class Program
{

const int PROCESS_WM_READ = 0x0010;
[DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

    [DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(int hProcess, 
      int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

    static void Main()
    {
    using (StreamWriter writer = new StreamWriter("O:\\out.txt"))
    {
        Console.SetOut(writer);
        Act();
    }
    }

    static void Act()
 {
        Process process = Process.GetProcessesByName("notepad")[0]; 
        IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id); 

        int bytesRead = 0;
        byte[] buffer = new byte[24]; 

      ReadProcessMemory((int)processHandle, 0x0021AAD0, buffer, buffer.Length, ref bytesRead);


        Console.WriteLine(Encoding.Unicode.GetString(buffer));

    }
}

代码执行我需要但只执行一次:它读取值并将其存储在.txt中。如果更新了记事本的值,则.txt内容将保持不变,直到程序重新启动。如何在不重新启动程序的情况下反复更新? 来吧,伙计们,我已经到了一半!!

1 个答案:

答案 0 :(得分:0)

创建一个按间隔运行程序的计划任务。

编辑...

static void Main() 
{
    var totalRunTime = TimeSpan.FromDays(1); 
    var elapsed = 0;
    while (elapsed < totalRunTime.TotalMilliseconds) 
    { 
        using (StreamWriter writer = new StreamWriter("O:\\out.txt")) 
        { 
            Console.SetOut(writer); 
            Act(); 
        } 
        Threading.Thread.Sleep(350);
        elapsed += 350;
    }
}