まもなく、アプリケーションからWindowsサービスをインストールしようとしています。そのようなものを試してみてください。
public void InstallWindowsService(string servicePath)
{
var isElevated = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
if (isElevated)
{
ExecuteCommand("cmd.exe", string.Format("{0} {1}", InstallUtilArguments(), servicePath));
}
else
{
throw new Exception("You should run the application as Administrator.");
}
}
private string InstallUtilArguments()
{
string framework = @"\Microsoft.NET\Framework\v4.0.30319\installutil.exe";
if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))// for 64 bit Windows OS
{
framework = @"\Microsoft.NET\Framework64\v4.0.30319\installutil.exe";
}
return Environment.GetEnvironmentVariable("windir") + framework;
}
private string ExecuteCommand(string fileName, string arguments)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
UseShellExecute = false,
};
using (Process proc = new Process())
{
proc.StartInfo = processStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
if (string.IsNullOrEmpty(output))
{
output = proc.StandardError.ReadToEnd();
}
return output;
}
}
は、オペレーティングシステム(64ビットまたは32ビット)を決定し、それが上昇し、アプリケーションを実行するユーザである検出InstallUtil
引数を構築し、そしてcmd.exe
使用してそれを実行します。
InstallWindowsService(@"C:\YourService\YourService.exe");
これまでにお試しいただいた内容をお見せください。 – Rinav
どのように私はあなたを見せることができますか?本当に申し訳ない。私はあなたのポイントを得ることができませんでした。 プライベートvoid button_Click(オブジェクト送信者、EventArgs e) { string key = p_key.Text; (キー== "列")もし {// SERVIC をインストール}他 {フォーム2のFRM =新しいフォーム2( "失敗")。 frm.Show(); } } – Ain
Rinavはあなたがあなた自身が問題を研究し解決するために何らかの努力をしたことを示すと予想されるので、通常はオンになっています。通常は、自分で試してみることなく、人の時間を求めているように見える場合は、多くの(または良質の)回答を得られません。したがって、この機能を実装しようとした場合は、コードを表示して、どの部分が機能していないかを説明してください。 – ADyson