2010-04-15 67 views
4

我正在使用mysettings來保存用戶設置。更改保存mysettings的路徑 - VB.NET 2008

這個配置文件保存在這個路徑:

C:\ Documents和 設置\ \ [本地 設置]應用 數據\\ \

可能改變這條道路?例如,在我的情況下,我將應用程序數據保存在「ProgramData」文件夾(Vista & W7)中,我希望將此配置文件保存在同一個文件夾中。有可能嗎?

在此先感謝

回答

0

從我的經驗,如果你說你將Win XP的轉移設置Vista或W7,這是不可能修復的文件夾路徑。

但是在一臺PC中,您可以通過使用sn工具對您的.exe進行簽名,從而將文件夾路徑修復爲ApplicationData \ ApplicationName \ anUgLycOde \。 (每次你重建和簽名都會阻止這個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