captain lizard
captain lizard

Reputation: 533

How to form a file path in C# under Mono in Mac OS?

I am trying to execute

Process.Start(s);

in C# in Mono Framework (in Mac OS).

but it seems I am unable to build string s correctly using this code:

var appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
Path.DirectorySeparatorChar + appName + Path.DirectorySeparatorChar;
var tempDir = appDataDir + "temp" + Path.DirectorySeparatorChar;
var s = "file://" + tempDir + "test.html";

I am getting the following error: "Error while executing: file://C:\users\my_username\Application Data\my_app_name\temp\test.html. Message: DDE failure."

Upvotes: 1

Views: 10251

Answers (2)

SushiHangover
SushiHangover

Reputation: 74144

You can stay away from DirectorySeparatorChar and let the Mono/.Net framework do the path parsing and combining for you:

var appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Console.WriteLine (appDataDir);
var tempDir = Path.Combine (appDataDir, "temp");
Console.WriteLine (tempDir);
var s = Path.Combine (tempDir, "test.html");
Console.WriteLine (s);
Process.Start (s);

If you are on OS-X, that will open "file:///Users/administrator/.config/temp/test.html" in your default browser.

Upvotes: 2

Rajesh B
Rajesh B

Reputation: 63

Path.DirectorySeparatorChar should return "/" in Linux and OS X and that is the correct way to form path.

Just FYI, Path.DirectorySeparatorChar should return "\" this.

Upvotes: 1

Related Questions