如何用mozilla在相对路径创建文件
时间:2006-03-23 来源:wolfssss
研究了一下午终于研究出来了,别的不多少,来点实在的:
function defaultFilterRule()
{
var streamOut = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
var dirService = Components.classes['@mozilla.org/file/directory_service;1']
.getService(Components.interfaces.nsIProperties);
var ruleFile = dirService.get("UChrm", Components.interfaces.nsIFile); // lxr.mozilla.org/seamonkey/source/xpcom/io/nsAppDirectoryServiceDefs.h
ruleFile.append(gFilterRuleFileName); // "appends" the file-string to our dir file-obj
if(ruleFile.exists() == true) {
ruleFile.remove(true);
}
ruleFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666); // uniquely name file
var rule="<rules>n";
rule = rule + " <rule>n";
rule = rule + " </rule>n";
rule = rule + "</rules>n";
// if the file is writable, append ruleString
if (ruleFile.isWritable()) {
streamOut.init(ruleFile, 0x04|0x08, 420, 0);
//streamOut.flush();
streamOut.write(rule, rule.length);
streamOut.close();
}
}
指定生成文件位置的关键在这一句:
var ruleFile = dirService.get("UChrm", Components.interfaces.nsIFile); // lxr.mozilla.org/seamonkey/source/xpcom/io/nsAppDirectoryServiceDefs.h
<a href="http://lxr.mozilla.org/seamonkey/source/xpcom/io/nsAppDirectoryServiceDefs.h">nsAppDirectoryServiceDefs.h</a>
对应的C++方法为:
nsCOMPtr servMan;
nsresult rv = NS_GetServiceManager(getter_AddRefs(servMan));
if (NS_FAILED(rv))
return -1;
nsCOMPtr directoryService;
rv = servMan->GetServiceByContractID(NS_DIRECTORY_SERVICE_CONTRACTID,
NS_GET_IID(nsIProperties),
getter_AddRefs(directoryService));
if (NS_FAILED(rv))
return -1;
nsCOMPtr theFile;
rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
NS_GET_IID(nsIFile),
getter_AddRefs(theFile));
if (NS_FAILED(rv)) return -1;
function defaultFilterRule()
{
var streamOut = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
var dirService = Components.classes['@mozilla.org/file/directory_service;1']
.getService(Components.interfaces.nsIProperties);
var ruleFile = dirService.get("UChrm", Components.interfaces.nsIFile); // lxr.mozilla.org/seamonkey/source/xpcom/io/nsAppDirectoryServiceDefs.h
ruleFile.append(gFilterRuleFileName); // "appends" the file-string to our dir file-obj
if(ruleFile.exists() == true) {
ruleFile.remove(true);
}
ruleFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666); // uniquely name file
var rule="<rules>n";
rule = rule + " <rule>n";
rule = rule + " </rule>n";
rule = rule + "</rules>n";
// if the file is writable, append ruleString
if (ruleFile.isWritable()) {
streamOut.init(ruleFile, 0x04|0x08, 420, 0);
//streamOut.flush();
streamOut.write(rule, rule.length);
streamOut.close();
}
}
指定生成文件位置的关键在这一句:
var ruleFile = dirService.get("UChrm", Components.interfaces.nsIFile); // lxr.mozilla.org/seamonkey/source/xpcom/io/nsAppDirectoryServiceDefs.h
<a href="http://lxr.mozilla.org/seamonkey/source/xpcom/io/nsAppDirectoryServiceDefs.h">nsAppDirectoryServiceDefs.h</a>
对应的C++方法为:
nsCOMPtr servMan;
nsresult rv = NS_GetServiceManager(getter_AddRefs(servMan));
if (NS_FAILED(rv))
return -1;
nsCOMPtr directoryService;
rv = servMan->GetServiceByContractID(NS_DIRECTORY_SERVICE_CONTRACTID,
NS_GET_IID(nsIProperties),
getter_AddRefs(directoryService));
if (NS_FAILED(rv))
return -1;
nsCOMPtr theFile;
rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
NS_GET_IID(nsIFile),
getter_AddRefs(theFile));
if (NS_FAILED(rv)) return -1;
相关阅读 更多 +