Visit Mozilla.org

nsDirectoryService

From MDC

« XPCOM API Reference

Contents

[edit] Summary

The XPCOM Directory Service. This service returns the locations of "well known" directories in an OS-independent manner. For instance, it can give you the path of the system's temporary directory, desktop directory, current working directory, and so on.


ClassID
f00152d0-b40b-11d3-8c9c-000064657374
ContractID
@mozilla.org/file/directory_service;1

[edit] Supported Interfaces

nsIProperties, nsIDirectoryService

[edit] Remarks

This component is a singleton and should therefore be accessed via the XPCOM Service Manager.

[edit] Example Code

#include "nsXPCOM.h"
#include "nsCOMPtr.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIServiceManager.h"
#include "nsIProperties.h"

/**
 * Get the location of the system's "temp" directory.
 */
nsresult GetTempDir(nsIFile **aResult)
{
  nsresult rv;

  nsCOMPtr<nsIServiceManager> svcMgr;
  rv = NS_GetServiceManager(getter_AddRefs(svcMgr));
  if (NS_FAILED(rv))
    return rv;

  nsCOMPtr<nsIProperties> directory;
  rv = svcMgr->GetServiceByContractID("@mozilla.org/file/directory_service;1",
                                      NS_GET_IID(nsIProperties),
                                      getter_AddRefs(directory));
  if (NS_FAILED(rv))
    return rv;
    
  rv = directory->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile), aResult);

  return rv;
}

Note that NS_OS_TEMP_DIR is defined in nsDirectoryServiceDefs.h along with a number of other Directory Service keys.

[edit] See Also

Using nsIDirectoryService, nsIProperties::get, nsIDirectoryServiceProvider