This documentation is archived and is not being maintained.

FileWebRequest Class

Provides a file system implementation of the WebRequest class.

Namespace:  System.Net
Assembly:  System (in System.dll)

public class FileWebRequest : WebRequest, 

The FileWebRequest type exposes the following members.

Protected methodFileWebRequest Obsolete. Initializes a new instance of the FileWebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.

Public propertyAuthenticationLevelGets or sets values indicating the level of authentication and impersonation used for this request. (Inherited from WebRequest.)
Public propertyCachePolicyGets or sets the cache policy for this request. (Inherited from WebRequest.)
Public propertyConnectionGroupNameGets or sets the name of the connection group for the request. This property is reserved for future use. (Overrides WebRequest.ConnectionGroupName.)
Public propertyContentLengthGets or sets the content length of the data being sent. (Overrides WebRequest.ContentLength.)
Public propertyContentTypeGets or sets the content type of the data being sent. This property is reserved for future use. (Overrides WebRequest.ContentType.)
Public propertyCredentialsGets or sets the credentials that are associated with this request. This property is reserved for future use. (Overrides WebRequest.Credentials.)
Public propertyHeadersGets a collection of the name/value pairs that are associated with the request. This property is reserved for future use. (Overrides WebRequest.Headers.)
Public propertyImpersonationLevelGets or sets the impersonation level for the current request. (Inherited from WebRequest.)
Public propertyMethodGets or sets the protocol method used for the request. This property is reserved for future use. (Overrides WebRequest.Method.)
Public propertyPreAuthenticateGets or sets a value that indicates whether to preauthenticate a request. This property is reserved for future use. (Overrides WebRequest.PreAuthenticate.)
Public propertyProxyGets or sets the network proxy to use for this request. This property is reserved for future use. (Overrides WebRequest.Proxy.)
Public propertyRequestUriGets the Uniform Resource Identifier (URI) of the request. (Overrides WebRequest.RequestUri.)
Public propertyTimeoutGets or sets the length of time until the request times out. (Overrides WebRequest.Timeout.)
Public propertyUseDefaultCredentialsInfrastructure. Always throws a NotSupportedException. (Overrides WebRequest.UseDefaultCredentials.)

Public methodAbortCancels a request to an Internet resource. (Overrides WebRequest.Abort().)
Public methodBeginGetRequestStreamBegins an asynchronous request for a Stream object to use to write data. (Overrides WebRequest.BeginGetRequestStream(AsyncCallback, Object).)
Public methodBeginGetResponseBegins an asynchronous request for a file system resource. (Overrides WebRequest.BeginGetResponse(AsyncCallback, Object).)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodEndGetRequestStreamEnds an asynchronous request for a Stream instance that the application uses to write data. (Overrides WebRequest.EndGetRequestStream(IAsyncResult).)
Public methodEndGetResponseEnds an asynchronous request for a file system resource. (Overrides WebRequest.EndGetResponse(IAsyncResult).)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetObjectDataPopulates a SerializationInfo with the data needed to serialize the target object. (Overrides WebRequest.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetRequestStreamReturns a Stream object for writing data to the file system resource. (Overrides WebRequest.GetRequestStream().)
Public methodGetResponseReturns a response to a file system request. (Overrides WebRequest.GetResponse().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Explicit interface implemetationPrivate methodISerializable.GetObjectDataInfrastructure. Populates a SerializationInfo object with the required data to serialize the FileWebRequest.

The FileWebRequest class implements the WebRequest abstract base class for Uniform Resource Identifiers (URIs) that use the file:// scheme to request local files.

Do not use the FileWebRequest constructor. Use the WebRequest.Create method to initialize new instances of the FileWebRequest class. If the URI scheme is file://, the Create method returns a FileWebRequest object.

The GetResponse method makes a synchronous request for the file specified in the RequestUri property and returns a FileWebResponse object that contains the response. You can make an asynchronous request for the file using the BeginGetResponse and EndGetResponse methods.

When you want to write data to a file, the GetRequestStream method returns a Stream instance to write to. The BeginGetRequestStream and EndGetRequestStream methods provide asynchronous access to the write data stream.

The FileWebRequest class relies on the File class for error handling and code access security.

The following code example uses the FileWebRequest class to access a file system resource.

// This example creates or opens a text file and stores a string in it. 
// Both the file and the string are passed by the user.
// Note. For this program to work, the folder containing the test file
// must be shared, with its permissions set to allow write access. 
using System.Net;
using System;
using System.IO;
using System.Text;

namespace Mssc.PluggableProtocols.File
    class TestGetRequestStream
        private static FileWebRequest myFileWebRequest;

        private static void showUsage ()
            Console.WriteLine ("\nPlease enter file name and timeout :");
            Console.WriteLine ("Usage: cs_getrequeststream <systemname>/<sharedfoldername>/<filename> timeout");
            Console.WriteLine ("Example: cs_getrequeststream ngetrequestrtream() ndpue/temp/hello.txt  1000");
            Console.WriteLine ("Small time-out values (for example, 3 or less) cause a time-out exception.");

        private static void makeFileRequest (string fileName, int timeout)
                // Create a Uri object. 
                Uri myUrl = new Uri ("file://" + fileName);

                // Create a FileWebRequest object.
                myFileWebRequest = (FileWebRequest)WebRequest.CreateDefault (myUrl);

                // Set the time-out to the value selected by the user.
                myFileWebRequest.Timeout = timeout;

                // Set the Method property to POST  
                myFileWebRequest.Method = "POST";
            catch (WebException e)
                Console.WriteLine ("WebException: " + e.Message);
            catch (UriFormatException e)
                Console.WriteLine ("UriFormatWebException: " + e.Message);

        private static void writeToFile ()
                // Enter the string to write to the file.
                Console.WriteLine ("Enter the string you want to write:");

                string userInput = Console.ReadLine ();

                // Convert the string to a byte array.
                ASCIIEncoding encoder = new ASCIIEncoding ();
                byte[] byteArray = encoder.GetBytes (userInput);

                // Set the ContentLength property.
                myFileWebRequest.ContentLength = byteArray.Length;

                string contentLength = myFileWebRequest.ContentLength.ToString ();

                Console.WriteLine ("\nThe content length is {0}.", contentLength);

                // Get the file stream handler to write to the file.
                Stream readStream = myFileWebRequest.GetRequestStream ();

                // Write to the file stream. 
                // Note.  For this to work, the file must be accessible
                // on the network. This can be accomplished by setting the property
                // sharing of the folder containg the file. 
                // FileWebRequest.Credentials property cannot be used for this purpose.
                readStream.Write (byteArray, 0, userInput.Length);
                Console.WriteLine ("\nThe String you entered was successfully written to the file.");

                readStream.Close ();
            catch (WebException e)
                Console.WriteLine ("The WebException: " + e.Message);
            catch (UriFormatException e)
                Console.WriteLine ("The UriFormatWebException: " + e.Message);

        public static void Main (String[] args)
            if (args.Length < 2)
                showUsage ();
                makeFileRequest (args[0], int.Parse (args[1]));
                writeToFile ();

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.