NS_NewNativeLocalFile

  • Revision slug: NS_NewNativeLocalFile
  • Revision title: NS_NewNativeLocalFile
  • Revision id: 187866
  • Created:
  • Creator: Jshin
  • Is current revision? No
  • Comment /* Remarks */ add warning against this function

Revision Content

{{ Xpcomapi() }}

Summary

The NS_NewNativeLocalFile function creates an instance of nsILocalFile that provides a platform independent representation of a file path.

#include "nsXPCOM.h"
#include "nsILocalFile.h"
 
nsresult NS_NewNativeLocalFile(
  const nsACString& aPath,
  PRBool aFollowLinks,
  nsILocalFile** aResult
);

Parameters

aPath
{{ mediawiki.external('in') }} A string object that specifies an absolute filesystem path. This string should be encoded using ASCII or the multibyte character coding corresponding to the native filesystem. This path does not need to reference an existing file. It is an error to pass a relative filesystem path.
aFollowLinks
{{ mediawiki.external('in') }} This attribute will determine if the nsILocalFile instance will automatically resolve symbolic links. This parameter has no effect on UNIX systems. On Windows, passing true causes shortcuts to be automatically resolved, and on MacOS, passing true causes finder aliases to be automatically resolved.
aResult
{{ mediawiki.external('out') }} A reference to the newly created nsILocalFile instance.

Return Values

The NS_NewNativeLocalFile function returns NS_OK if successful. Otherwise, it returns an error code.

NS_ERROR_FILE_UNRECOGNIZED_PATH
Indicates that the specified path is invalid. This error is returned if a relative file path is passed to NS_NewNativeLocalFile.

Remarks

On UNIX systems, the prefix "~/" is supported as a shorthand for the user's home directory. If you use this function on Windows 2000 or later, you would not be able to handle file names containing characters outside the default code page even though the OS has no problem dealing with them. Do not use this function (on Windows) unless it is guaranteed that the full path involved is always ASCII.

Example Code

// Create a local file that references c:\foo.txt
nsresult rv;
nsCOMPtr<nsILocalFile> file;
rv = NS_NewNativeLocalFile(nsEmbedCString("c:\\foo.txt"), PR_FALSE,
                           getter_AddRefs(file));
if (NS_FAILED(rv))
  return rv;

Here, nsEmbedCString is used to convert the ASCII string literal to an object that can be passed as a const nsACString& parameter.

History

This function was finalized for Mozilla 1.0. See {{ Bug("129279") }} for details.

See Also

nsILocalFile, nsIFile

Revision Source

<p> {{ Xpcomapi() }}
</p>
<h3 id="Summary" name="Summary"> Summary </h3>
<p>The <code>NS_NewNativeLocalFile</code> function creates an instance of <code><a href="en/NsILocalFile">nsILocalFile</a></code> that provides a platform independent representation of a file path.
</p>
<pre class="eval">#include "nsXPCOM.h"
#include "nsILocalFile.h"
 
nsresult NS_NewNativeLocalFile(
  const nsACString&amp; <var>aPath</var>,
  PRBool <var>aFollowLinks</var>,
  nsILocalFile** <var>aResult</var>
);
</pre>
<h3 id="Parameters" name="Parameters"> Parameters </h3>
<dl><dt> aPath
</dt><dd> {{ mediawiki.external('in') }} A string object that specifies an absolute filesystem path. This string should be encoded using ASCII or the multibyte character coding corresponding to the native filesystem. This path does not need to reference an existing file. It is an error to pass a relative filesystem path.
</dd></dl>
<dl><dt> aFollowLinks
</dt><dd> {{ mediawiki.external('in') }} This attribute will determine if the <code><a href="en/NsILocalFile">nsILocalFile</a></code> instance will automatically resolve symbolic links. This parameter has no effect on UNIX systems. On Windows, passing true causes shortcuts to be automatically resolved, and on MacOS, passing true causes finder aliases to be automatically resolved.
</dd></dl>
<dl><dt> aResult
</dt><dd> {{ mediawiki.external('out') }} A reference to the newly created <code><a href="en/NsILocalFile">nsILocalFile</a></code> instance.
</dd></dl>
<h3 id="Return_Values" name="Return_Values"> Return Values </h3>
<p>The <code>NS_NewNativeLocalFile</code> function returns <code>NS_OK</code> if successful. Otherwise, it returns an error code.
</p>
<dl><dt> NS_ERROR_FILE_UNRECOGNIZED_PATH
</dt><dd> Indicates that the specified path is invalid. This error is returned if a relative file path is passed to <code>NS_NewNativeLocalFile</code>.
</dd></dl>
<h3 id="Remarks" name="Remarks"> Remarks </h3>
<p>On UNIX systems, the prefix "~/" is supported as a shorthand for the user's home directory. If you use this function on Windows 2000 or later, you would not be able to handle file names containing characters outside the default code page even though the OS has no problem dealing with them. Do not use this function (on Windows) unless it is guaranteed that the full path involved is always ASCII.
</p>
<h3 id="Example_Code" name="Example_Code"> Example Code </h3>
<pre>// Create a local file that references c:\foo.txt
nsresult rv;
nsCOMPtr&lt;nsILocalFile&gt; file;
rv = NS_NewNativeLocalFile(nsEmbedCString("c:\\foo.txt"), PR_FALSE,
                           getter_AddRefs(file));
if (NS_FAILED(rv))
  return rv;
</pre>
<p>Here, <code><a href="en/NsEmbedCString">nsEmbedCString</a></code> is used to convert the ASCII string literal to an object that can be passed as a <code>const nsACString&amp;</code> parameter.
</p>
<h3 id="History" name="History"> History </h3>
<p>This function was finalized for Mozilla 1.0. See {{ Bug("129279") }} for details.
</p>
<h3 id="See_Also" name="See_Also"> See Also </h3>
<p><code><a href="en/NsILocalFile">nsILocalFile</a></code>,
<code><a href="en/NsIFile">nsIFile</a></code>
</p>
Revert to this revision