如何在进程上重定向标准输入而不立即退出进程?
我创建了一个实用程序来锁定文件 lockfile.exe
,它接受两个参数:fileName(要锁定的文件)和 fileShare(要共享的文件)应用)。然后,我从另一个应用程序中调用该实用程序,以测试处理锁定文件时的错误情况。 lockfile.exe
实用程序使用指定的共享锁定文件,然后等待任何字符输入以释放锁定并退出。
在我的测试应用程序中,我创建了一个具有两个方法 LockFile
和 UnlockFile
的类 FileLocker
,如下所示:
public class FileLocker
{
private Process process;
public void LockFile(string fileName)
{
this.process = new Process();
this.process.StartInfo.RedirectStandardInput = true;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.FileName = "lockfile.exe";
this.process.StartInfo.Arguments =
"\"" + fileName + "\" " + FileShare.Read.ToString();
this.process.Start();
}
public void UnlockFile()
{
this.process.StandardInput.Write('X');
this.process.StandardInput.Flush();
this.process.WaitForExit();
this.process.Close();
}
}
当我调用方法 LockFile
时code> 进程启动后立即退出。它不会等待 UnlockFile
中的输入。当我不重定向标准输入时,进程将等待键盘输入。
我需要更改/修复什么,以便该进程不会立即退出。我需要它等待 UnlockFile
中提供的输入,然后进程才应该退出?
更新:
更新以显示上面的 FileLocker
类并提供下面的示例调用:
class Program
{
static void Main(string[] args)
{
FileLocker locker = new FileLocker();
locker.LockFile("HelloWorld.txt");
locker.UnlockFile();
}
}
I created a utility to lock files lockfile.exe
, which accepts two parameters: fileName (file to be locked) and fileShare (share to be applied). I then call this utility from within another application to test error conditions when processing locked files. The lockfile.exe
utility locks a file with the specified share and then waits for any character input to release the lock and exit.
In my test application I created a class FileLocker
with two methods LockFile
and UnlockFile
as follows:
public class FileLocker
{
private Process process;
public void LockFile(string fileName)
{
this.process = new Process();
this.process.StartInfo.RedirectStandardInput = true;
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.CreateNoWindow = true;
this.process.StartInfo.FileName = "lockfile.exe";
this.process.StartInfo.Arguments =
"\"" + fileName + "\" " + FileShare.Read.ToString();
this.process.Start();
}
public void UnlockFile()
{
this.process.StandardInput.Write('X');
this.process.StandardInput.Flush();
this.process.WaitForExit();
this.process.Close();
}
}
When I call the method LockFile
the process immediately exits after starting. It does not wait for the input in UnlockFile
. When I do not redirect the standard input, then the process waits for keyboard input.
What do I need to change/fix, so that the process does not immediately exit. I need it to wait for the input provided in UnlockFile
and only then should the process exit?
Update:
Updated to show FileLocker
class above and to provide sample calls below:
class Program
{
static void Main(string[] args)
{
FileLocker locker = new FileLocker();
locker.LockFile("HelloWorld.txt");
locker.UnlockFile();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您在 lockfile.exe 应用程序中使用
Console.ReadKey()
。它不接受来自标准输入的输入,而是使用低级键盘挂钩来检查无法从其他应用程序发送的按键。我使用仅包含一个简单的
Console.ReadLine()
的 LockFile.exe 测试了您的程序,如果我更改Write('X')
,它会按预期工作调用WriteLine("X")
(因为调用 readline 在返回之前需要换行符)。所以我怀疑你的问题出在lockfile.exe程序而不是这个程序上。确保您正在使用Console.ReadLine()
等待 lockfile.exe 应用程序中标准输入的输入。The problem is likely that you are using
Console.ReadKey()
in your lockfile.exe application. This does not accept input from standard input but uses low-level keyboard hooks to check for key presses which you cannot send from another application.I tested your program with a LockFile.exe that just contained a simple
Console.ReadLine()
, and it works just as expected if I change theWrite('X')
call to aWriteLine("X")
(since the call to readline needs a newline before returning). So I'm suspecting your problem is in the lockfile.exe program rather than this one. Ensure you are usingConsole.ReadLine()
to wait for input from standard input in your lockfile.exe application.