Application.UserAppDataPath Property


Gets the path for the application data of a user.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Shared ReadOnly Property UserAppDataPath As String

Property Value

Type: System.String

The path for the application data of a user.

If a path does not exist, one is created in the following format:

Base Path\CompanyName\ProductName\ProductVersion

Data stored in this path is part of user profile that is enabled for roaming. A roaming user works on more than one computer in a network. The user profile for a roaming user is kept on a server on the network and is loaded onto a system when the user logs on. For a user profile to be considered for roaming, the operating system must support roaming profiles and it must be enabled.

A typical base path is C:\Documents and Settings\username\Application Data. This path will be different, however, if the Windows Forms application is deployed by using ClickOnce. ClickOnce creates its own application data directory that is isolated from all other applications. For more information, see Accessing Local and Remote Data in ClickOnce Applications.

The following code example displays two forms and exits the application when both forms are closed. When the application starts and exits, the position of each form is remembered. This example demonstrates using the UserAppDataPath property to store application data for the user.

The class MyApplicationContext inherits from ApplicationContext and keeps track when each form is closed, and exits the current thread when they both are. The class stores the positions of each form for the user. The form position data is stored in a file titled Appdata.txt that is created in the location determined by UserAppDataPath. The Main method calls Application.Run(context) to start the application given the ApplicationContext.

This code is an excerpt from the example shown in the ApplicationContext class overview. Some code is not shown for the purpose of brevity. See ApplicationContext for the whole code listing.

Public Sub New()
    formCount = 0

    ' Handle the ApplicationExit event to know when the application is exiting.
    AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

        ' Create a file that the application will store user specific data in.
        userData = New FileStream(Application.UserAppDataPath + "\appdata.txt", FileMode.OpenOrCreate)

    Catch e As IOException
        ' Inform the user that an error occurred.
        MessageBox.Show("An error occurred while attempting to show the application." + _
                        "The error is:" + e.ToString())

        ' Exit the current thread instead of showing the windows.
    End Try

    ' Create both application forms and handle the Closed event
    ' to know when both forms are closed.
    form1 = New AppForm1()
    AddHandler form1.Closed, AddressOf OnFormClosed
    AddHandler form1.Closing, AddressOf OnFormClosing
    formCount = formCount + 1

    form2 = New AppForm2()
    AddHandler form2.Closed, AddressOf OnFormClosed
    AddHandler form2.Closing, AddressOf OnFormClosing
    formCount = formCount + 1

    ' Get the form positions based upon the user specific data.
    If (ReadFormDataFromFile()) Then
        ' If the data was read from the file, set the form
        ' positions manually.
        form1.StartPosition = FormStartPosition.Manual
        form2.StartPosition = FormStartPosition.Manual

        form1.Bounds = form1Position
        form2.Bounds = form2Position
    End If

    ' Show both forms.
End Sub

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    ' When the application is exiting, write the application data to the
    ' user file and close it.

        ' Ignore any errors that might occur while closing the file handle.
    End Try
End Sub

.NET Framework
Available since 1.1
Return to top