简体   繁体   中英

Config File Location for any User, Workstation specific config

Where is the correct location to store a machine specific config file. The file changes from time to time and it needs to be accessible to any user (so one config per machine). I don't want to modify the app.config, since it gets overwritten on a new release and due to permission reasons. The file will be created at first start and remains there for any new version. Regards

The .NET framework already uses a machine config file, which contains settings that are shared by all .NET applications that run on that machine, but it doesn't sound like that's what you are looking for.

If you don't need the application to be able to make modifications to the config file, then I'd say that the easiest way would just be to store the settings in an XML file in the executable folder. You can easily load the settings in the application from its current directory. You could name the file anything you want.

If, however, you need to make modifications to the config file from within the application, then I wouldn't recommend doing it that way, since not every user will have the permissions necessary to write to a file in the application folder. So, if that's the case, I would recommend storing the config file in isolated storage.

Isolated storage can get a little tricky to make sure that your storage doesn't get "lost" when you change the version number of your application. And it gets even a little trickier when you want to store settings that are accessible from any application running on the machine. In the past, I needed to make an isolated storage that would be shared by multiple applications in a suite of software, so I wrote the following class, which works well for that purpose:

Public Class LocalStorage
    Implements ILocalStorage

    Private Const _CompanyUrl As String = "http://www.mycompany.com"

    Public Function Read(Of T)(ByVal fileName As String) As T
        Dim contents As T = Nothing
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(T))
        Try
            Using stream As Stream = New IsolatedStorageFileStream(fileName, FileMode.Open, FileAccess.Read, GetStorage())
                Using xmlReader As XmlReader = New XmlTextReader(stream)
                    contents = CType(serializer.Deserialize(xmlReader), T)
                End Using
            End Using
        Catch ex As FileNotFoundException
        Catch ex As Exception
            Throw New IOException("Failed to read from " + fileName + " in local isolated storage", ex)
        End Try
        Return contents
    End Function

    Public Sub Write(Of T)(ByVal fileName As String, ByVal serializableDataObject As T)
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(T))
        Try
            Using stream As Stream = New IsolatedStorageFileStream(fileName, FileMode.Create, FileAccess.Write, GetStorage())
                Using xmlTextWriter As XmlTextWriter = New XmlTextWriter(stream, New UTF8Encoding(False))
                    xmlTextWriter.Formatting = Formatting.Indented
                    Dim namespaces As XmlSerializerNamespaces = New XmlSerializerNamespaces()
                    namespaces.Add("", "")
                    serializer.Serialize(xmlTextWriter, serializableDataObject, namespaces)
                End Using
            End Using
        Catch ex As Exception
            Throw New IOException("Failed to write to " & fileName & " in local isolated storage", ex)
        End Try
    End Sub

    Private Function GetStorage() As IsolatedStorageFile
        Return IsolatedStorageFile.GetStore(IsolatedStorageScope.Machine Or IsolatedStorageScope.Assembly, Nothing, New Url(_CompanyUrl))
    End Function
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM