C# 根据用户的选择将控制台应用程序移动到后台
我在标题中看到了一些解决这个问题的方法,但它们都不是我想要的 我有ch=Console.ReadKey.KeyChar,如果用户输入“y”,我希望应用程序进入后台。如果用户输入“n”,程序将继续运行,就像什么都没有发生一样。这可能吗?我做了很多研究,但还是找不到适合我的 以下是我到目前为止所拥有的:C# 根据用户的选择将控制台应用程序移动到后台,c#,background,console-application,C#,Background,Console Application,我在标题中看到了一些解决这个问题的方法,但它们都不是我想要的 我有ch=Console.ReadKey.KeyChar,如果用户输入“y”,我希望应用程序进入后台。如果用户输入“n”,程序将继续运行,就像什么都没有发生一样。这可能吗?我做了很多研究,但还是找不到适合我的 以下是我到目前为止所拥有的: char ch = '0'; Console.WriteLine("Enter Log File Destenation:"); string url = Console.ReadLine(); C
char ch = '0';
Console.WriteLine("Enter Log File Destenation:");
string url = Console.ReadLine();
Console.WriteLine("Run In BackGround ? (Defaul Set to False)";
ch = Console.ReadKey().KeyChar;
if (ch == 'y')
{
//Move To Background
}
// continue with program
这是一个比特币汇率记录器。它从网站标题中获取比特率,并将其记录到txt文件中,该文件的目标由用户在程序开始时设置。
设置目标后,它会询问是否应该在后台运行。
无论哪种方式,程序都会进入一个whiletrue循环以获取日志。使用参数HWND\u BOTTOM的方法
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out W32RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct W32RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public const uint HWND_BOTTOM = 1;
static void Main(string[] args)
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
W32RECT rect;
GetWindowRect(handle , out rect); //to get position and size of your console
SetWindowPos(handle, IntPtr.Zero, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, HWND_BOTTOM);//to set background position of your console with the same size and screen's position
}
}
}
没有测试,但它会工作
来源:如果你需要,我可以翻译。我想你做不到。你最好的办法可能是把你发布的代码变成一个启动程序,调用在后台运行的应用程序。你能更具体一点吗?我不懂这个方法