尝试在控制台应用程序中自动化进程

时间:2017-11-14 11:47:29

标签: winapi console-application

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

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

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

我已经创建了一个filewatcher,现在正试图弄清楚如何使代码工作,然后才能成为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

1 个答案:

答案 0 :(得分:0)

显然这个问题是由于filesystemwatcher是单线程引起的,当我尝试启动我的新进程时,它会阻止旧的进程运行。我需要做的就是删除一个新的runpano函数并从我的代码调用它,它现在正在工作。

相关问题