Cambio de ruta para guardar MySettings - VB.NET 2008
-
27-09-2019 - |
Pregunta
Estoy utilizando MySettings para guardar la configuración del usuario.
Este archivo de configuración se guarda en este camino:
C: \ Documents y Settings \ [\ locales Configuración] Solicitud Datos \\ \
¿Es posible cambiar esta ruta? Por ejemplo, en mi caso puedo guardar datos de aplicaciones en la carpeta "Datos de programa" (Vista y W7) y me gustaría guardar este archivo de configuración en la misma carpeta. ¿Es posible?
Gracias de antemano
Solución
A partir de mi experiencia, si usted dice que va a transferir la configuración de Windows XP a Vista o W7, no es posible fijar la ruta de la carpeta.
Sin embargo, en un PC, puede corregir la ruta a la carpeta ApplicationData \ ApplicationName \ anUgLycOde \ por el signo de su .exe mediante el uso de herramientas sn. (El código UGlu cambiaría cada vez que vuelve a generar y el signo previene que este).
Pero si se piensa hacer una versión cruz de victorias, sugiero que no utilice mi configuración, sino que utilizan la serialización XML. Crear una clase para definir su configuración, cargar y guardar mediante el uso de XML serializar y deserializar. Usted puede poner en Mis documentos o misma carpeta como * .exe.
Aquí está la muestra:
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
Para usarlo simplemente como:
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