0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

hostsを管理者権限で開いてみる

Last updated at Posted at 2019-04-10

#hostsファイルを管理者権限で開くソースを、.Net標準のコンパイラで実行形式にしてみる
コンパイラ(C#やvb)が標準で入っている事を教えてもらったのでメモのため。

##前提
ソースはC#で記述している

##コンパイラの説明を出すコマンドは以下
D:\D-MyData\data> c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc

補足:vbのコンパイラ
D:\D-MyData\data> c:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc

##ソース

hostsEdit.cs
using System;
using System.Diagnostics;

namespace test
{
	partial class StartWrapProgram
	{
		public static void Main ( string [ ] args )
		{
			string [ ] LoadModuleAtt = { @"notepad.exe", @"RunAs" };
			var mmm = new StartWrapProgram ( );
			mmm.VarbFunc ( LoadModuleAtt );
		}
	}
	partial class StartWrapProgram
	{
		private void VarbFunc ( string [ ] loadModuleAt )
		{
			var proc = new Process ( );
			proc.StartInfo.FileName=  loadModuleAt [ 0 ];
			proc.StartInfo.Verb = loadModuleAt [ 1 ];			// 管理者として実行
			var tgt = @"\Windows\System32\drivers\etc\hosts";
			proc.StartInfo.Arguments = tgt;
			try {
				proc.Start ( );
				proc.WaitForExit ( );
				proc.Close ( );
			}
			catch ( Exception e ) {
				Exception dmy = e;
			}
		}
	}
}

##コンパイル実施
D:\D-MyData\data> c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc /platform:x64 /t:winexe hostsEdit.cs

以下のようにコンパイル後は表示。

Microsoft (R) Visual C# Compiler Version 4.0.30319.36392
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

##コンパイル後に出来上がったもの確認
D:\D-MyData\data>dir hostsEdit.exe

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?