簡體   English   中英

XPCOM組件中如何查看目錄分隔符

[英]How to check the directory separator in XPCOM component

我試圖從谷歌找到答案,但我失敗了。
我熟悉python,其中我們可以通過“os.path.sep”檢查平台特定的目錄分隔符。

XPCOM 組件中是否有方法或某些常量可以指示路徑分隔符?
或者有沒有辦法規范特定於平台的路徑?

例如,用戶在window下輸入一個文件路徑:C:/path1/path2/test.txt
如果我不使路徑平台有效,使用 nsILocalFile::initWithPath 將引發異常。 有效路徑應為“C:\path1\path2\test.txt”。 所以我想知道有沒有辦法使路徑有效並可以初始化 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);
}

您可以將 nsIIOService.newURI 與 file:// URI 一起使用,並返回一個 nsIFileURL object,您可以從中返回一個 nsIFile object。 您可以從 nsIFile object 中獲取路徑信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM