题
是否有任何命令行或 .NET 方法可以在后台运行一个进程,隐藏它尝试打开的任何窗口?
已经尝试过:
var process = new Process()
{
StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = TheRealExecutableFileNameHere
}
}
process.Start();
到目前为止还没有成功
其他提示
我回顾我的代码,它看起来几乎相同,你的:
ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments)
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true
};
Process process = Process.Start(psi);
的唯一显着的区别(比格式化和PSI构造我们选择了其他)是我使用UseShellExecute和RedirectStandardOutput作为我需要读取然处理的结果。
我已经发现上面的代码一致运行在XP和Vista隐藏进程。我还发现,不过,你可能会遇到相同的,即一个隐藏的进程可能会揭开序幕,在默认情况下是不是隐藏的另一种方法。换句话说,如果你开始隐藏进程A和处理,进而揭开序幕流程B,你无法控制的方法B的显示方式。可能会显示你没有控制权的Windows。
我希望这有助于一点。好运。
没有 我不知道一个纯粹的.Net 方法来实现这一点。
然后我想到了内核 工作对象, ,但没有找到类似的选项 用户界面限制.
那么,下一个(尚未证实)想法是创建比挂起的进程, 创建一个 Windows 钩子 然后,它将监视 调用WndProc 并过滤掉WM_SHOW消息。(然后,当然,恢复进程,在单独的线程中等待,直到它终止,删除钩子)
我知道这已经回答了,但你可能会迫使一个窗口去隐藏与非托管调用FindWindow函数和的ShowWindow。
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
psi = new ProcessStartInfo(); // etc..
some_process = Process.Start(psi);
System.Threading.Thread.Sleep(50); // need give the window a chance to be created
IntPtr hWnd = FindWindow(null, "name of the window");
if (hWnd != IntPtr.Zero) ShowWindow(hWnd, 0); // 0 = SW_HIDE
相反缺憾。
这取决于你是否要启动的应用程序最小化,但允许在需要它来与用户进行交互,或者如果你想禁止使用,无论发生什么情况,用户的所有访问。
如果是后者,则可以不同的桌面上下文与当前用户下运行的过程。
不同的桌面环境中使用,例如,通过登录对话框,并通过Vista的UAC - 任何发生在一个桌面环境中运行是独立于其他
可能是大锤的方法来您的问题虽然。
我注意到,如果CreateNoWindow = FALSE确实当文件名指向一个Windows可执行文件,如果你有机会到winform应用程序的源代码,绝对没有,那么你或许可以提供控制默认命令行参数形式的可见度,并做在winform应用程序启动代码是这样的:
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form1 = new Form1();
form1.Load += new EventHandler((s,o) =>
{
//check if the form should be shown based on command line arg
if (args.Contains("dontShowWindow"))
{
//hide it
form1.ShowInTaskbar = false;
form1.Visible = form1.ShowInTaskbar = false;
}
}
);
Application.Run(form1);
}
在你调用代码,现在可以指定“dontShowWindow”作为一个过程参数:
ProcessStartInfo info = new ProcessStartInfo
{
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
FileName = @"C:\temp\testWinForm.exe",
Arguments = "dontShowWindow"
};
Process.Start(info);
希望这有助于
一个非常简单的方式实现这一目标是创建一个服务帐户,并通过Windows任务计划程序服务帐户的用户的上下文中运行可执行文件。
您可以使用此CodeProject上设置计划任务:
http://www.codeproject.com/KB/cs/tsnewlib.aspx
您可以编程域或本地计算机与C#创建服务帐户很容易。
http://www.codeproject.com/KB/system/OSUserMangement.aspx http://support.microsoft.com/kb/306273
运行在另一个用户的,除非该用户已登录不交互显示的上下文计划任务进程;因此,使用服务帐户的。
public Form1()
{
InitializeComponent();
Form1 fm = new Form1();
fm.ShowInTaskbar = false;
fm.Visible = fm.ShowInTaskbar = false;
}
伟大的作品!
我假设您想要一个在运行时用户不可见的进程。
您可以尝试以下操作,看看这是否是您想要的。
- 创建一个持续运行的简单控制台应用程序(并启动它进行测试)。
- 右键单击项目 --> 属性 --> 应用程序选项卡 --> 输出类型 --> 将其从“控制台应用程序”更改为 Windows 应用程序。
- 再次启动同一个应用程序(看看这是否是您想要的)。
- 通过 Windows 任务管理器关闭应用程序:)
该进程似乎出现在任务管理器中,但在任务栏中不可见。Alt+TAB 无法调出此进程。
不过,我只是希望您没有制作任何恶意应用程序:)
我想你可能想尝试是创建一个新的Windows站。看看这篇文章是在MSDN上解释了有关他们一点。
http://msdn.microsoft.com /en-us/library/ms681928(VS.85).aspx
您可以做一个Windows的服务。服务试图打开的任何窗口只运行在会话0,这意味着没有用户能看到它。这也适用于所有Windows不过,或许这不是你在找什么。
下面的代码为我工作: 早先的处理(记事本),用于闪烁桌面屏幕上要打印的记事本,但它使用户分心,所以下面的代码隐藏记事本过程中的背景。
System.Windows.Forms.PrintDialog printDialog = new PrintDialog();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(Application.StartupPath + "/PrintingDocketFile/PrintCustomerOrder.txt");
psi.Verb = "PRINT";
//Process.Start(psi);
//psi.CreateNoWindow = true;
psi.UseShellExecute = false;
//---------------------------------------------------
psi.CreateNoWindow = false;
psi.UseShellExecute = true;
psi.FileName = Application.StartupPath + "/PrintingDocketFile/PrintCustomerOrder.txt";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.Arguments = @"%windir%\system32\notepad.exe";
//Process.Start(psi);
//------------------------------------------------------
/////////////////////----------------------------------
printProcess.StartInfo = psi;
/////psi.CreateNoWindow = true;
//psi.FileName = "Application.StartupPath" + "/PrintingDocketFile/PrintCustomerOrder.txt";
//p.StartInfo.FileName = "Notepad.EXE";
//p.StartInfo.Arguments = "/i /q \"" + installationPackages[i] + "\"";
printProcess.Start();
/////////////////////////-------------------------------
while (!printProcess.HasExited) ;
if (!printProcess.HasExited)
{
printProcess.Close();
}