winterTTr
winterTTr

Reputation: 1768

How to check the directory separator in XPCOM component

I am trying to find the answer from google, but i am failed.
I am familiar with python, in which we can check the platform-specific directory separator by "os.path.sep".

Is there a way or some constant in XPCOM component that can indicate the path separator?
Or is there a way to normalize the path specific to platform?

For example, user input a file path under window: C:/path1/path2/test.txt
Using nsILocalFile::initWithPath will throw exception if I do not make the path platform valid. The valid path should be "C:\path1\path2\test.txt". So I wonder there is way to make a path valid and can init the nsILocalFile.

Upvotes: 0

Views: 644

Answers (2)

bhovhannes
bhovhannes

Reputation: 5689

/**
 * Returns file path separator under current OS
 */
function getPathSeparator()
{
    var profD = Components.classes["@mozilla.org/file/directory_service;1"].
                getService(Components.interfaces.nsIProperties).
                get("ProfD", Components.interfaces.nsIFile);
    profD.append("abc");
    profD.append("abc");
    var length = profD.path.length;
    return profD.path.substr(length-("abc".length)-1,1);
}

Upvotes: 2

ehsan
ehsan

Reputation: 236

You can use nsIIOService.newURI with a file:// URI, and get a nsIFileURL object back, from which you can get an nsIFile object back. You can get the path information from the nsIFile object.

Upvotes: 0

Related Questions