Alterar caminho para salvar mysettings - vb.net 2008
-
27-09-2019 - |
Pergunta
Estou usando o MySettings para salvar as configurações do usuário.
Este arquivo de configuração é salvo neste caminho:
C: Documentos e Configurações [Configurações locais] Dados do aplicativo\
É possível mudar esse caminho? Por exemplo, no meu caso, salvo dados do aplicativo na pasta "ProgramData" (Vista & W7) e gostaria de salvar esse arquivo de configuração na mesma pasta. É possível?
desde já, obrigado
Solução
Pela minha experiência, se você disser que transferirá a configuração do Win XP para Vista ou W7, não é possível corrigir o caminho da pasta.
No entanto, em um PC, você pode corrigir o caminho da pasta para ApplicationData ApplicationName anuglycode por assinar seu .exe usando ferramentas SN. (O código UGLU mudaria cada vez que você se reconstruirá e assinará isso).
Mas se você pensa em fazer uma versão cruzada, sugiro que você não use minhas configurações, mas para usar a serialização XML. Crie uma classe para definir sua configuração, carregar e salvá -la usando o XML Serialize e Desserialize. Você pode colocar no meu documento ou na mesma pasta com *.exe.
Aqui está a amostra:
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 usá -lo simplesmente 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