[英]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".我熟悉python,其中我们可以通过“os.path.sep”检查平台特定的目录分隔符。
Is there a way or some constant in XPCOM component that can indicate the path separator? XPCOM 组件中是否有方法或某些常量可以指示路径分隔符?
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例如,用户在window下输入一个文件路径:C:/path1/path2/test.txt
Using nsILocalFile::initWithPath will throw exception if I do not make the path platform valid.如果我不使路径平台有效,使用 nsILocalFile::initWithPath 将引发异常。 The valid path should be "C:\path1\path2\test.txt".
有效路径应为“C:\path1\path2\test.txt”。 So I wonder there is way to make a path valid and can init the nsILocalFile.
所以我想知道有没有办法使路径有效并可以初始化 nsILocalFile。
/**
* 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);
}
You can use nsIIOService.newURI with a file:// URI, and get a nsIFileURL object back, from which you can get an nsIFile object back.您可以将 nsIIOService.newURI 与 file:// URI 一起使用,并返回一个 nsIFileURL object,您可以从中返回一个 nsIFile object。 You can get the path information from the nsIFile object.
您可以从 nsIFile object 中获取路径信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.