【发布时间】: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