▼ ps.bat ( PowerShell をそのまま使えない場合は以下のバッチファイルを作成して使用します )@powershell -NoProfile -ExecutionPolicy Unrestricted "./%1.ps1"▼ build.ps1
Add-Type -path "wget.cs" ` -ReferencedAssemblies System.Web, System.Windows.Forms ` -OutputAssembly my_wget.exe ` -OutputType ConsoleApplication Read-Host "何かキーを押してください"
wget.cs は、wget.ps1 と同じフォルダにあります
` で継続行指定です。
System.Web, System.Windows.Forms という感じで複数の指定を行っています
▼ wget.cs
第一引数に渡した URL をダウンロードします。
Environment.GetCommandLineArgs() の結果には、自分自身が含まれています。
よって、param[1] と args[0] が同じ内容になります。( 後述の PowerShell から実行した場合は、param には PowerShell への引数がセットされます )
Visual Studio で追加参照が必要なクラスが -ReferencedAssemblies の対象です
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Windows.Forms;
public class Program
{
public static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string[] param = Environment.GetCommandLineArgs();
if (param.Length > 1)
{
Console.WriteLine( string.Format("第一引数 : {0}", param[1]) );
Console.WriteLine(string.Format("第一引数 : {0}", args[0]));
}
else
{
MessageBox.Show("ダウンロードする URL を引数に指定して下さい");
Environment.Exit(0);
}
string localFileName = Path.GetFileName(param[1]);
Console.WriteLine(string.Format("ファイル名 : {0}", localFileName));
using( WebClient wc = new WebClient() ) {
wc.DownloadFile( param[1], localFileName );
}
// *******************************************
// -ReferencedAssemblies の複数テスト用
// *******************************************
string percent_encoding = HttpUtility.UrlEncode(param[1]);
Console.WriteLine( percent_encoding );
MessageBox.Show("処理が終了しました");
}
}
MessageBox.Show HttpUtility.UrlEncode ▼ 実行用バッチファイルのサンプルmy_wget.exe https://winofsql.jp/image/planet.jpg▼ 実行結果の表示C:\user\ps\cs>my_wget.exe http://winofsql.jp/image/planet.jpg 第一引数 : http://winofsql.jp/image/planet.jpg 第一引数 : http://winofsql.jp/image/planet.jpg ファイル名 : planet.jpg http%3a%2f%2fwinofsql.jp%2fimage%2fplanet.jpgこの後、メッセージボックスが表示されます PowerShell として exe なしで実行する場合 ▼ run.ps1
Param($url) Write-Host $url Add-Type -path "wget.cs" ` -ReferencedAssemblies System.Web, System.Windows.Forms [Program]::Main($url) Read-Host "何かキーを押してください"
実行
powershell -NoProfile -ExecutionPolicy Unrestricted run.ps https://winofsql.jp/image/planet.jpg
スクリプトセットのダウンロード関連するドキュメント Read-Host コマンドレットの使用 Add-Type
|
|
【PowerShell + C#の最新記事】
- nuget.exe CLI を使用してパッケージをダウンロードし、C# のソースコードで利用して PowerShell でビルドする
- PowerShell で VisualStudio で作成した Form アプリケーションをビルドする( DataGridView に select 文の結果を表示する / MySQL )
- PowerShell で VisualStudio で作成した Form アプリケーションをビルドする( Form アプリケーションを テキストエディタのみで作成するテンプレート )
- TKMP + imap + C# + PowerShell : メールボックス(階層)の一覧表示
- nuget.exe + SMO + PowerShell + C# : テーブルの CREATE TABLE 文 を取得
- PowerShell( 実質C# )を使用して、ファイルの分割を行う
- PowerShell( 実質C# )を使用して、MessageBox の応答でバッチファイルの処理を変化させる
- PowerShell で C# のソースコード(get_rec_mysql.cs) を使用して System.Data.Odbc で MySQL のデータを一覧表示( csv )







