0 out of 3 rated this helpful - Rate this topic

SPWeb.GetFile Method (String)

Retrieves the file object located at the specified URL.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
public SPFile GetFile(
	string strUrl
)

Parameters

strUrl
Type: System.String
The URL for the file.

Return Value

Type: Microsoft.SharePoint.SPFile
The file object with the specified URL.
Exception Condition
ArgumentNullException

strUrl is null.

ArgumentException

strUrl is or empty.

This method calls the SPFile constructor with this and strUrl.

The following code example loads the default.aspx file.


SPFile file = web.GetFile(“default.aspx”);
    if(file.Exists) // here, this condition will return always false
    {
      // …
    }
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Inconsistent, be careful

Given a sharepoint site where a file is at http://myserver/somesite/docs/myfile.txt you CAN open the file with: $0new SPSite("http://myserver/").RootWeb.GetFile("somesite/docs/myfile.txt")$0 $0All looks dandy, file Exists=true. But if you try to call SaveBinary(byte[]) on the file it throws a DirectoryNotFoundException!$0 $0But if you open the file from the "somesite" SPWeb object instead, it will upload your file as expected.$0 $0$0 $0 $0Remember SPWeb.GetFile only works for files on that web.$0

=

Irrelevant Remarks
Most irrelevant remarks I have seen for an API documentation. What has it got with the original API? It is far from relevant. Does anyone do proof-reading or oversee how the sentences are constructed?