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

C# 如何从其他应用程序获取值并将其写入文件?,c#,C#,我正在尝试制作一个没有UI的简单应用程序,它将从另一个应用程序获取数据,并使用C将其存储在输出文件中。仅此而已。我怎样才能做到: 1.打开流程 2.求X的值 2.1求Y值 2.2找到值Z 3.将值写入文件C:\output.txt 4.例如,每1000毫秒更新一次 这里有两个我几乎无法运行的代码。从应用程序读取: 写入文件: 问题2: 1.如何连接这两个代码 2.如何使程序每1秒/2秒/300秒刷新一次输出 谢谢你的帮助! 另外,如果你知道任何现有的应用程序会做完全相同的事情-这对我来说是可以的

我正在尝试制作一个没有UI的简单应用程序,它将从另一个应用程序获取数据,并使用C将其存储在输出文件中。仅此而已。我怎样才能做到: 1.打开流程 2.求X的值 2.1求Y值 2.2找到值Z 3.将值写入文件C:\output.txt 4.例如,每1000毫秒更新一次

这里有两个我几乎无法运行的代码。从应用程序读取:

写入文件:

问题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内容将保持不变,直到程序重新启动。如何在不重新启动程序的情况下使其不断更新?
来吧,伙计们,我已经走到一半了

创建按时间间隔运行程序的计划任务

编辑

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;
    }
}

几乎没有选择。。。1.在app.config中添加轮询设置,将代码包装在循环中,并在循环结束时使用轮询设置超时。2.跳过循环,创建一个每X分钟运行一次程序的计划任务。选项2非常方便!让我试试@ragerory选项2不适合我,因为调度程序在1分钟内循环启动。我需要循环大约0,3sec…1应该足够了。计划任务的最小间隔为1分钟,而我需要大约350ms。app.config工作正常,但是否可以在不添加外部文件的情况下修改代码?也许是为了;;构造可以解决我的问题。我建议将设置放在app.config中,这样它就不会硬编码,但我的答案是一样的,你可以将它包装在一个循环中,也许一段时间 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)); } }
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;
    }
}