簡単にファイルチェックサム(MD5)ユーティリティを作る方法
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:winexe /out:%1.v20.exe %1
@pause
csc.exeはVisual C#のコンパイラです。//file-name: Program.cs
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace easyMD5
{
class Program
{
private static byte[] _GetBytes(FileStream fs, int len)
{
System.Security.Cryptography.HashAlgorithm alg=null;
try
{
//MD5以外にはSHA1,SHA256,SHA384,SHA512
alg = System.Security.Cryptography.MD5.Create();
Byte[] buf=new Byte[len];
fs.Read(buf,0,len);
return alg.ComputeHash(buf,0,len);
}
catch (Exception e) { MessageBox.Show(e.Message); }
finally {if(alg!=null)alg.Clear();}
return null;
}
private static string _BytesToHexString(byte[] bytes, char separator)
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
for(int i=0;i<bytes.Length;i++)
{
sb.Append(String.Format("{0:X2}",bytes[i]));
if(i<(bytes.Length-1))
sb.Append(separator);
}
return sb.ToString();
}
private static string GetMD5(string path)
{
if (!File.Exists(path))
return null;
FileStream fs = null;
try
{
fs = new FileStream(path, FileMode.Open, FileAccess.Read);
Byte[] bs = _GetBytes(fs, (int)(fs.Length));
return _BytesToHexString(bs, ':');
}
catch (Exception e){ MessageBox.Show(e.Message); }
finally {if (fs!=null)fs.Close();}
return null;
}
private static string GetFileSizeString(string path)
{
FileInfo fi = new FileInfo(path);
long lsize = fi.Length;
double d;
string r=lsize.ToString("N0") + " Byte";
if (lsize<1024)
return r;
else if (1024<=lsize && lsize<(1024*1024))
{//KB
d = Math.Floor(((double)lsize / 1024.0) * 100.0) / 100.0;
return d.ToString("N2") + " KB" + " (" + r + ")";
}
//MB
d = Math.Floor(((double)lsize / (1024.0 * 1024.0)) * 100.0) / 100.0;
return d.ToString("N2") + " MB" + " (" + r + ")";
}
[STAThread]
static void Main(string[] args)
{
String s;
String s_title="easyMD5 ファイルチェックサム";
if (args.Length == 1 && File.Exists(args[0]))
{
s = Path.GetFileName(args[0]) + " : size " + GetFileSizeString(args[0]) + "\n" +
GetMD5(args[0]) +
"\n\n" +Path.GetDirectoryName(args[0]);
s_title = "easyMD5 : [OK]でクリップボードへ";
DialogResult r;
r = MessageBox.Show(s, s_title,
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button2);
if (r == DialogResult.OK)//[OK]ボタンでクリップボード
Clipboard.SetText(s, TextDataFormat.UnicodeText);
return;
}
if (args.Length == 0)
s = "usage:\n" +
" easyMD5 [file path]\n\n" +
"プログラムアイコンにファイルをドラッグ&ドロップでも起動\n" +
"SendToフォルダにショートカットを作成する事で使い勝手も良くなります";
else
s = "このツールは複数ファイルやフォルダには対応していません";
MessageBox.Show(s, s_title);
}
}
}
以上の内容を丸ごとコピーしメモ帳などでProgram.csと名前をつけ保存します。@if exist %windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe goto dotNET_V20
@if exist %windir%\Microsoft.NET\Framework\v3.5\csc.exe goto dotNET_V35
@echo .NET Framework 2.0(v2.0.50727)もしくは3.5(v3.5)が無いのでコンパイルできません
@dir /AD %windir%\Microsoft.NET\Framework
@goto END
:dotNET_V20
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:winexe /optimize+ /out:%1.v20.exe %1
@goto END
:dotNET_V35
%windir%\Microsoft.NET\Framework\v3.5\csc.exe /target:winexe /optimize+ /out:%1.v35.exe %1
@goto END
:END
@pause
以上の内容を丸ごとコピーしメモ帳などでcsc_compile.batと名前をつけ保存します。2011 settyo top page