Changer le chemin pour sauver MySettings - VB.NET 2008
-
27-09-2019 - |
Question
J'utilise MySettings pour enregistrer les paramètres utilisateur.
Ce fichier de configuration est enregistré dans ce chemin:
c: \ Documents and Settings \ \ [Local Paramètres] Application Les données \\ \
est possible de changer ce chemin? Par exemple, dans mon cas, je enregistrer les données d'application dans le dossier « ProgramData » (Vista et W7) et je voudrais sauver ce fichier de configuration dans le même dossier. Est-ce possible?
Merci à l'avance
La solution
D'après mon expérience, si vous dites que vous allez transférer le réglage de Win XP à Vista ou W7, il est impossible de fixer le chemin du dossier.
Toutefois, dans un PC, vous pouvez corriger le chemin du dossier à ApplicationData \ ApplicationName \ anUgLycOde \ par votre signe .exe en utilisant des outils de sn. (Le code uglu changerait à chaque fois que vous reconstruisez et signe empêcherez cela).
Mais si vous pensez à faire une version cross Win, je vous suggère de ne pas utiliser les paramètres Mon mais d'utiliser sérialisation Xml. Créer une classe pour définir vos paramètres, Charger et sauvegarder en utilisant Xml sérialisation et la désérialisation. Vous pouvez mettre dans mon document ou même dossier avec * .exe.
Voici l'exemple:
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
Pour l'utiliser simplement comme:
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