4

ユーザー設定を保存するために mysettings を使用しています。

この構成ファイルは、次のパスに保存されます。

c:\ Documents and Settings \ \ [ローカル設定] アプリケーション データ\\\

このパスを変更することは可能ですか? たとえば、私の場合、アプリ データを「ProgramData」フォルダー (Vista & W7) に保存し、この構成ファイルを同じフォルダーに保存したいと考えています。可能です?

前もって感謝します

4

1 に答える 1

0

私の経験から、設定をWin XPからVistaまたはW7に転送すると言う場合、フォルダパスを修正することはできません。

ただし、1台のPCでは、snツールを使用して.exeに署名することにより、ApplicationData \ ApplicationName \anUgLycOde\へのフォルダーパスを修正できます。(ugluコードは、再構築して署名するたびに変更されるため、これを防ぐことができます)。

ただし、クロスWinバージョンを作成する場合は、[マイ設定]ではなく、Xmlシリアル化を使用することをお勧めします。設定を定義するクラスを作成し、XmlSerializeおよびDeserializeを使用してロードおよび保存します。*.exeを使用してマイドキュメントまたは同じフォルダに配置できます。

サンプルは次のとおりです。

Imports System.Xml.Serialization

<XmlRoot("FTPSender")> _
Public Class FTPSenderConfig

    ' default file path relative to the current .exe file path.
    Const fDefaultCFGFile As String = "FTPSender.cfg"
    Public Shared ReadOnly Property DefaultFilePath() As String
        Get
            Return IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) & "\" & fDefaultCFGFile
        End Get
    End Property

    Public Shared Function Load(Optional ByVal FilePath As String = Nothing) As FTPSenderConfig
        If FilePath Is Nothing Then FilePath = DefaultFilePath()
        If Not IO.File.Exists(FilePath) Then Return New FTPSenderConfig() ' load default settings
        Using sr As New IO.StreamReader(FilePath)
            Try
                Dim x As New XmlSerializer(GetType(FTPSenderConfig))
                Load = CType(x.Deserialize(sr), FTPSenderConfig)
                'MyLog.WriteLog("FTPSender settings loaded.")
            Finally
                If sr IsNot Nothing Then
                    sr.Close()
                    sr.Dispose()
                End If
            End Try
        End Using
    End Function

    Public Shared Sub Save(ByVal FTPSenderConfig As FTPSenderConfig, Optional ByVal FilePath As String = Nothing)
        If FilePath Is Nothing Then FilePath = DefaultFilePath()
        If FTPSenderConfig Is Nothing Then Return
        Using sw As New IO.StreamWriter(FilePath)
            Try
                Dim x As New XmlSerializer(FTPSenderConfig.GetType())
                x.Serialize(sw, FTPSenderConfig)
                'MyLog.WriteLog("FTPSender settings saved.")
            Finally
                If sw IsNot Nothing Then
                    sw.Close()
                    sw.Dispose()
                End If
            End Try
        End Using
    End Sub

        Dim fHost As String = "127.0.0.1"
        <XmlElement("Host")> _
        Public Property Host() As String
            Get
                Return fHost
            End Get
            Set(ByVal value As String)
                fHost = value
            End Set
        End Property

        Dim fUser As String = "guess"
        <XmlElement("User")> _
        Public Property User() As String
            Get
                Return fUser
            End Get
            Set(ByVal value As String)
                fUser = value
            End Set
        End Property

        Dim fPassEncrypted As String = EncDec.Encrypt("guess")
        <XmlElement("PassEncrypted")> _
        Public Property PassEncrypted() As String
            Get
                Return fPassEncrypted
            End Get
            Set(ByVal value As String)
                fPassEncrypted = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property Pass() As String
            Get
                Return EncDec.Decrypt(fPassEncrypted)
            End Get
            Set(ByVal value As String)
                fPassEncrypted = EncDec.Encrypt(value)
            End Set
        End Property

        Dim fTransferMode As String = MyFTPClient.TransferModeEnum.Passive.ToString()
        <XmlElement("TransferMode")> _
        Public Property TransferMode() As MyFTPClient.TransferModeEnum
            Get
                Return [Enum].Parse(GetType(MyFTPClient.TransferModeEnum), fTransferMode)
            End Get
            Set(ByVal value As MyFTPClient.TransferModeEnum)
                fTransferMode = value.ToString()
            End Set
        End Property

End Class

単に次のように使用するには:

Dim cfg As FTPSenderConfig

cfg = FTPSenderConfig.Load() ' In Form_Load

Dim h as String = cfg.Host
cfg.Host = h

FTPSenderConfig.Save(cfg) ' In Form_FormClosed
于 2010-04-15T17:44:42.880 に答える