更改路径保存mysettings - VB.NET 2008
-
27-09-2019 - |
题
我使用mysettings保存的用户设置。
此配置文件被保存在这个路径:
C:\ Documents和 设置\ \ [本地 设置]应用 数据\\ \
有可能改变这个路径?例如,在我来说,我保存应用程序数据“ProgramData”文件夹(Vista和W7),我想保存在同一文件夹这个配置文件。是可能的?
在预先感谢
解决方案
这是我的经验,如果你说你将Win XP的转移设置Vista或W7,这是不可能修复的文件夹路径。
然而,在一台PC,您可以通过标志使用SN工具修复的文件夹路径的ApplicationData \应用程序名称\ anUgLycOde \您的.exe文件。 (该uglu代码将每次重建和符号将防止这种变化)。
不过,如果你认为做一个交叉win版,我建议你不要用我的设置,但使用XML序列化。创建一个类通过使用XML序列化和反序列化来定义您的设置,加载和保存它。你可以把我的文件或* .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
不隶属于 StackOverflow