Launch a local webpage on a Windows machine?

I’m using Application.OpenURL() to launch a webpage that exists on the user’s hard drive. I can get this to work under OS X:

var newURL ="file://localhost" + Application.dataPath +"/../../MyFolder/" + "webpage/mypage.html";
newURL = Regex.Replace(newURL, " ", "%20");
Application.OpenURL(newURL);

But why won’t this work under Windows?

var newURL ="file://localhost" + Application.dataPath +"\\..\\MyFolder\\" + "webpage/mypage.html";
newURL = Regex.Replace(newURL, " ", "%20");
Application.OpenURL(newURL);

I also tried “file:\localhost” but that didn’t work either. Can anyone help?

Try “file:///C:/” (or whatever is your drive letter) instead of “file://localhost”.

BR,
Juha

The problem with that is that I won’t know what the drive is, that’s why I’m using Application.dataPath. The Windows code works fine when I use it for writing a screen shot or reading a data file, but launching a web page it doesn’t work.

“localhost” tries to connect to a web server running on the machine. Most Windows users don’t have webservers running on their machines though.

What you should try is using Application.dataPath, convert backward slashes to forward slashes, and append “file:///” in front.

OK. That was just a thought…

Just curious (as I don’t have currently access to Windows with Unity apps, but will be trying something like you soon), what is the value of string “Application.dataPath”? Does it contain the full path including drive letter in Windows?

BR,
Juha

Yes (see the docs)

I’ve got everything but converting backwards slashes to forward slashes working. I tried this:

NewURL = Regex.Replace(newURL, "\", "%47");

but it results in errors. I tried it without converting the backwards slashes and it seems to work with Firefox on Windows XP. Do you know if having mixed forward + backward slashes will be a problem for other browsers? If so, any hints on how to convert this?

"" is an escape character, so I think that should be “\”.

Edit: OK, that didn’t work either…some problem with the regular expression syntax, apparently, which I don’t know about…

–Eric

Use the verbatim string literal @ to ignore escape characters.

myString.Replace(@"\", @"/"));

Also, for a bit more flexibility, you can make use of shell commands, like ‘open’, using System.Diagnostics.

switch (Application.platform)
     {
      case RuntimePlatform.WindowsPlayer:
            System.Diagnostics.Process.Start(PathAndFileName);
            break;
      default:
             System.Diagnostics.Process.Start(string.Format("open \"{0}\"", PathAndFileName));
              break;
     }

Thanks Shaun. Unfortunately, I’m getting errors when I try to use “@”, is this only usable with C#? (I’m writing in Javascript).

OK, I figured it out…since \ is an escape character, you need \ to actually represent a , but since \ is also an escape character in regular expressions, you need \ to match a \ there too. Got that? :wink:

NewURL = Regex.Replace(NewURL, "\\\\", "/");

–Eric

Eric to the rescue again! Thanks, works perfect!

Sorry about that - I should’ve mentioned it was C# only.
Thanks Eric :slight_smile: