【问题标题】:Trying to automate a process in console application尝试在控制台应用程序中自动化进程
【发布时间】:2017-11-14 11:47:29
【问题描述】:

我试图让我的控制台应用程序模拟拖放文件,到目前为止我还没有运气。

系统抛出一个 win32 异常,说明它无法找到该文件,因为我知道这不是真正的问题,我希望有人能阐明可能导致此行为的原因。

我怀疑它可能是 DEP。我可以拖放文件并且该过程按预期运行,但我需要自动执行此操作。

我已经创建了一个文件观察器,现在正试图弄清楚如何让代码工作,然后再将其设为 Windows 服务。

但现在我真的被这个 win32 错误困住了。

     public class Watcher
{
    public static void Main(string[] args)
    {
        Run();
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();


        // If a directory is not specified, exit program.
        if (args.Length != 2)
        {

           // Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)");

            return;
        }

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = args[1];
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch jpg files.
        watcher.Filter = "*.jpg";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        Process.Start(e.FullPath + "c:\\demo\\kr-pano\\mpr.bat");

}

访问链接以查看完整的 win32 异常详细信息。 Win32Exception

【问题讨论】:

  • 将异常文本完全添加到问题中。不要使用外部链接。
  • 我害怕格式化之神会打击我。我设法弄明白了。

标签: winapi console-application


【解决方案1】:

显然这个问题是因为文件系统观察器是单线程的,当我尝试启动我的新进程时,它会阻止旧进程运行。我需要做的就是存根一个新的 runpano 函数并从我的代码中调用它,它现在可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多