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 extends WebRequest implements ISerializable

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.

No code example is currently available or this language may not be supported.
// This program creates or open a text file in which it stores a string.
// Both file and string are passed by the user.
// Note. In order for this program to work, the folder containing the test file
// must be shared with its permissions set to allow write access.

#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net;

__gc class TestGetRequestStream {
	static FileWebRequest*  myFileWebRequest;

	static void showUsage() {
		Console::WriteLine(S"\nPlease enter file name and timeout :");

		Console::WriteLine(S"Usage: cs_getrequeststream <systemname>/<sharedfoldername>/<filename> timeout");
		Console::WriteLine(S"Example: cs_getrequeststream ndpue/temp/hello.txt 1000");
		Console::WriteLine(S"Small timeout values (for instance 3 or less) cause a timeout exception.");

	static void makeFileRequest(String* fileName, int timeout) {
		try {
			// Create a Uri object.
			Uri* myUrl = new Uri(String::Format(S"file://{0}", fileName));
			// Create a FileWebRequest object.
			myFileWebRequest = dynamic_cast<FileWebRequest*>(WebRequest::CreateDefault(myUrl));
			// Set the timeout to the value selected by the user.
			myFileWebRequest->Timeout = timeout;
			// Set the Method property to POST
		} catch (WebException* e) {
			Console::WriteLine(S"WebException: {0}", e->Message);
		} catch (UriFormatException* e) {
			Console::WriteLine(S"UriFormatWebException: {0}", e->Message);

	static void writeToFile() {
		try {
			// Enter the string to write into the file.
			Console::WriteLine(S"Enter the string you want to write:");
			String* userInput = Console::ReadLine();
			// Convert the string to Byte array.
			ASCIIEncoding* encoder = new ASCIIEncoding();
			Byte byteArray[] = encoder->GetBytes(userInput);
			// Set the ContentLength property.
			String* contentLength = myFileWebRequest->ContentLength.ToString();
			Console::WriteLine(S"\nThe content length is {0}.", contentLength);
			// Get the file stream handler to write into the file.
			Stream* readStream=myFileWebRequest->GetRequestStream();

			// Write to the file stream.
			// Note. In order 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. The permissions
			// can be set so everyone can modify the file.
			// FileWebRequest::Credentials property cannot be used for this purpose.
			readStream->Write(byteArray, 0, userInput->Length);

			Console::WriteLine(S"\nThe String you entered was successfully written into the file.");


		} catch (WebException* e) {
			Console::WriteLine(S"The WebException: {0}", e->Message);
		} catch (UriFormatException* e) {
			Console::WriteLine(S"The UriFormatWebException: {0}", e->Message);

	static void Main() {
		String* args[] = Environment::GetCommandLineArgs();
		if (args->Length < 3)
		else {
			makeFileRequest(args[1], Int32::Parse(args[2]));

int main()

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

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