更改保存我的设置的路径 - VB.NET 2008

发布于 2024-08-28 19:13:05 字数 244 浏览 5 评论 0原文

我正在使用 mysettings 来保存用户设置。

该配置文件保存在以下路径中:

c:\ 文件和 设置\\[本地 设置]应用程序 数据\\\

可以更改此路径吗?例如,在我的例子中,我将应用程序数据保存在“ProgramData”文件夹(Vista 和 W7)中,并且我想将此配置文件保存在同一文件夹中。有可能吗?

提前致谢

I am using mysettings to save user settings.

This config file is saved in this path:

c:\ Documents and
Settings \ \ [Local
Settings] Application
Data\\\

Is possible to change this path? For example, in my case I save app data in "ProgramData" folder (Vista & W7) and I would like save this config file in the same folder. Is possible?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2024-09-04 19:13:05

根据我的经验,如果您说要将设置从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

From my experience, If you say you will transfer the setting from Win XP to Vista or W7, It is not possible to fix the folder path.

However in one PC, You can Fix the folder path to ApplicationData\ApplicationName\anUgLycOde\ by sign your .exe by using sn tools. (the uglu code would change each time you rebuild and sign will prevent this).

But if you think to make a cross Win version, I suggest you not to use My Settings but to use Xml serialization. Create a class to define your setting, Load and Save it by using Xml Serialize and Deserialize. You can put in My Document or same folder with *.exe.

Here is the sample:

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

To use it simply as:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文