1
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 3 years have passed since last update.

【C#】ショートカットファイルをダウンロードしたい

Posted at

概要

画面のボタンクリックで適当なショートカットファイル(.url)をダウンロードする。

実装

HTML
<a type="submit" asp-controller="Sample" asp-action="DownloadShortCutFile" >Download</a>
CS
public class SampleController : Controller
{
	public IActionResult DownloadShortCutFile()
	{
	    // ファイル名
	    string fileName = "ShortCut.url";
	    //作成するURLショートカットのパス
	    string shortcutPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), fileName);
	    //ショートカットのリンク先
	    string url = "https://www.google.co.jp/";

	    //テキストファイルに書き込む
	    System.Text.Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
	    StreamWriter sw = new StreamWriter(shortcutPath, false, enc);
	    sw.WriteLine("[InternetShortcut]");
	    sw.WriteLine("URL=" + url);
	    sw.Close();

	    // ファイルダウンロード
	    var filePath = shortcutPath;
	    var file = System.IO.File.ReadAllBytes(filePath);
	    return File(file, MediaTypeNames.Application.Octet, fileName);
	}
}
1
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
1
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?