如何一起运行Windows窗体和控制台,以便它们可以交换数据? [英] How to run a windows form and console together so that they could exchange data?
本文介绍了如何一起运行Windows窗体和控制台,以便它们可以交换数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以这里的解决方案包括一个控制台应用程序和一个Windows窗体。
我使用多个启动选项同时运行它们。但控制台应用程序无法正常运行,因为它作为一个单元运行。
我尝试过:
这是使用System的代码
So here the solution consists of a Console Application and a Windows Form.
I have used multiple startup options to run them simultaneously. But the console app doesn't work as it was running as a single unit.
What I have tried:
This is the code
using System;
using System.IO;
using System.Security.Permissions;
public class Watcher
{
public static void Main()
{
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 text files.
watcher.Filter = "*.txt";
// 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);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
推荐答案
我为你搜索了visual studio multiple startup projects,这里是第一个结果
如何:设置多个启动项目 [ ^ ]
I googled "visual studio multiple startup projects" for you and here is the first result
How to: Set Multiple Startup Projects[^]
请参阅答案#3以 this [ ^ ]所以问题。
/ ravi
See answer #3 to this[^] SO question.
/ravi
这篇关于如何一起运行Windows窗体和控制台,以便它们可以交换数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文