LoginSignup
2
1

More than 5 years have passed since last update.

Siv3DでローカルファイルをOS規定の手段で開く方法

Posted at

やりたいこと

ゲームのドキュメント(html)にゲーム内からアクセスしたい。
つまり下記のような感じ。

main.cpp
if (Internet::IsConnected()) {
    Internet::LaunchWebBrowser(L"http://aini-bellwood.jp/game/signbordfactory/manual/index.html");
}
else {
    //ここでローカルのhtmlを読みたい
}

ためしたこと

Internet::LaunchWebBrowserにローカルファイルのパスを入れる

main.cpp
Internet::LaunchWebBrowser(FileSystem::InitialPath() + L"doc/index.html");

これはもちろん無理。
ドキュメントに「url:http:// もしくは https:// で始まる URL」と書いてある。

IEを外部プロセスとして立ち上げる

main.cpp
String tmpStr = FileSystem::InitialPath() + L"doc/index.html";
tmpStr = tmpStr.replace(L"/", L"\\");
Optional<ProcessInfo> process = System::CreateProcess(L"C:/Program Files/Internet Explorer/iexplore.exe", L"-new " + tmpStr);

一応動いた。でもこれはあまりにも乱暴なのでやりたくない…。
ここでTwitterに助けを求めた。

system関数を使う

main.cpp
system(".\\doc\\index.html");

なるほど!!かしこい!!!
でももうちょいスマートにならんかな…ということでReputeless氏に聞いてみた。

System::LaunchFile()を使う。

main.cpp
System::LaunchFile(L".\\doc\\index.html", LaunchVerb::Open);

はい!瞬殺!!
FileSystemしか見てなかったから全く気づかなかったよ…。

まとめ

ということでこんな形に落ち着いた。
今後もこれでいく。

main.cpp
if (Internet::IsConnected()) {
    Internet::LaunchWebBrowser(L"http://aini-bellwood.jp/game/signbordfactory/manual/index.html");
}
else {
    System::LaunchFile(L".\\doc\\index.html", LaunchVerb::Open);
}
2
1
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
2
1