RoleEnvironment.GetLocalResource Method (String)

 

Retrieves a specified local storage resource.

Namespace:   Microsoft.WindowsAzure.ServiceRuntime
Assembly:  Microsoft.WindowsAzure.ServiceRuntime (in Microsoft.WindowsAzure.ServiceRuntime.dll)

Syntax

[SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)]
public static LocalResource GetLocalResource(
    string localResourceName
)
public:
[SecurityPermissionAttribute(SecurityAction::Assert, UnmanagedCode = true)]
static LocalResource^ GetLocalResource(
    String^ localResourceName
)
[<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode = true)>]
static member GetLocalResource : 
        localResourceName:string -> LocalResource
<SecurityPermissionAttribute(SecurityAction.Assert, UnmanagedCode := True)>
Public Shared Function GetLocalResource (
    localResourceName As String
) As LocalResource

Parameters

  • localResourceName
    Type: System.String

    Type: System.String

    The name of the local storage resource that is defined in the ServiceDefiniton.csdef file.

Return Value

Type: Microsoft.WindowsAzure.ServiceRuntime.LocalResource

Type: Microsoft.WindowsAzure.ServiceRuntime.LocalResource

An instance of LocalResource that represents the local storage resource.

Exceptions

Exception Condition
RoleEnvironmentException

The local storage resource does not exist.

Remarks

The following code example shows how to use this method to retrieve the local storage resource and write a text file to the resource:

try
{
    // Retrieve an object that points to the local storage resource.
    LocalResource localResource = RoleEnvironment.GetLocalResource("localStoreTwo");

    // Define the file name and path.
    string[] paths = { localResource.RootPath, "MyStorageTest.txt" };
    String filePath = Path.Combine(paths);

    using (FileStream writeStream = File.Create(filePath))
    {
        Byte[] textToWrite = new UTF8Encoding(true).GetBytes("Testing Web role storage");
        writeStream.Write(textToWrite, 0, textToWrite.Length);
    }
}
catch (RoleEnvironmentException e)
{
    Console.WriteLine("The local resource isn't defined or doesn't exist. \n" + e.ToString());
}

See Also

RoleEnvironment Class
Microsoft.WindowsAzure.ServiceRuntime Namespace

Return to top