// downnoname.cs using System; using System.IO; using System.Net; using System.Text.RegularExpressions; class DownloadWithNoName { static void Main(string[] args) { if (args.Length == 0) return; string url = args[0]; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); string fileName = "download.tmp"; // ヘッダ情報からファイル名の取得 string dispos = res.Headers["Content-Disposition"]; if (!String.IsNullOrEmpty(dispos)) { // filename=<ファイル名>の抜き出し Regex re = new Regex(@" filename\s*=\s* (?: ""(?[^""]*)"" | (?[^;]*) ) ", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); Match m = re.Match(dispos); if (m.Success) { fileName = m.Groups["filename"].Value; } } // ファイルのダウンロード using (Stream st = res.GetResponseStream()) using (FileStream fs = new FileStream(fileName, FileMode.Create)) { Byte[] buf = new Byte[1024]; int count = 0; do { count = st.Read(buf, 0, buf.Length); fs.Write(buf, 0, count); } while (count != 0); } res.Close(); } } // コンパイル方法:csc downnoname.cs