簡體   English   中英

在運行程序時如何更改程序的目錄路徑

[英]How do I change the directory path of my program whilst its running

  Private Sub _2013results_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dBprovider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" 'This is the type of connection I've chose to link to the database
    'Change the following to your access database location
    datafile = "C:\Year 13\SheetFormats.accdb"
    connString = dBprovider & datafile
    myConnection.ConnectionString = connString
End Sub

特別是在“ datafile”行上,該行具有數據庫文件的路徑,這是要發送給其他學生,他們可能與我的路徑不同,因此有什么方法可以創建警報來允許用戶修改代碼以適合他們的路徑

希望這可以幫助您到達想要去的地方。 一旦熟悉了此代碼,就可以考慮將Private dbFilePath As String交換為用戶設置,以便每次用戶使用應用程序時都可以Private dbFilePath As String更改。

Imports System.IO

Public Class Form1

    Private dbFilePath As String = "" 'this is where I will store the path to the DB file

    Private Sub btn_Go_Click(sender As Object, e As EventArgs) Handles btn_Go.Click
        If dbFilePath = "" Then
            'if the DB path is empty, the user must provide one
            GetDbFilePath()
            btn_Go_Click(sender, e)
        Else
            'there is already a path
            _2013results_Load("", EventArgs.Empty)
            'on your TO DO list, implement code so that if the user already specified a path but he/she wants to update it.
        End If
    End Sub

    ''' <summary>
    ''' This sub will 'prompt' the user for a path
    ''' </summary>
    Private Sub GetDbFilePath()
        Dim message, title, defaultValue As String
        Dim myvalue As Object
        message = "Please enter the path of your db file"
        title = "DB path grabber"
        defaultValue = ""
        myvalue = InputBox(message, title, defaultValue)
        If myvalue IsNot "" AndAlso File.Exists(myvalue) Then
            'if the user entered something, and that something is a valid path
            dbFilePath = myvalue
        End If

    End Sub


    Private Sub _2013results_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If dbFilePath = "" Then
            Return
        End If
        Dim dBprovider As String
        dBprovider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" 'This is the type of connection I've chose to link to the database
        'Change the following to your access database location
        Dim connString = dBprovider & dbFilePath
        Dim myconnection As OleDb.OleDbConnection = New OleDb.OleDbConnection()
        myconnection.ConnectionString = connString
    End Sub


End Class

嗯...這里是使用用戶設置。 這是一個非常基本的實現,但是它幾乎不需要完成任何工作。 這種方式假設您有一個名為“ settings_DbFilePath”的用戶設置。 要創建這樣的設置...

在解決方案資源管理器->您的解決方案->您的項目->設置中

名稱:settings_DbFilePath
類型:字符串
范圍:用戶
值:

Private Sub GetDbFilePath_UserSettingsWay()
    Dim message, title, defaultValue As String
    Dim myvalue As Object
    message = "Please enter the path of your db file"
    title = "DB path grabber"
    defaultValue = ""
    myvalue = InputBox(message, title, defaultValue)
    If myvalue IsNot "" AndAlso File.Exists(myvalue) Then
        'if the user entered something, and that something is a valid path
        My.MySettings.Default.settings_DbFilePath = myvalue
        My.MySettings.Default.Save()
    End If

End Sub

Private Sub _2013results_Load_UserSettingsWay(sender As Object, e As EventArgs) Handles MyBase.Load
    If My.MySettings.Default.settings_DbFilePath = "" Then
        Return
    End If
    Dim dBprovider As String
    dBprovider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" 'This is the type of connection I've chose to link to the database
    'Change the following to your access database location
    Dim connString = dBprovider & My.MySettings.Default.settings_DbFilePath
    Dim myconnection As OleDb.OleDbConnection = New OleDb.OleDbConnection()
    myconnection.ConnectionString = connString
End Sub

這是第三個替代方法,因此您可以更好地了解其他兩種方法在做什么,因此可以查明應在代碼中的什么地方實現這些方法。 在此代碼段中,我基本上只是重構並向現有的_2013results_Load方法中添加了一些內容。 但是,這第三種選擇確實希望您已經創建了一個名為settings_DbFilePathstring類型的用戶設置,如選項#2中所述。

Private Sub _2013results_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dBprovider As String
    dBprovider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" 'This is the type of connection I've chose to link to the database

    'Now lets ask the user for the DB path
    Dim message, title, defaultValue As String 'initialize some strings
    Dim userAnswer As Object 'initialize an object
    message = "Please enter the path of your db file"
    title = "DB path grabber"
    defaultValue = ""
    userAnswer = InputBox(message, title, defaultValue) 'This line triggers the prompt
    If userAnswer IsNot "" AndAlso File.Exists(userAnswer) Then
        'if the user entered something, and that something is a valid path
        My.MySettings.Default.settings_DbFilePath = userAnswer 'park that value in the user setting
        My.MySettings.Default.Save() 'save the settings
    Else
        Return 'the user did not provide a valid path so get out of here without even attempting to connect!
    End If

    Dim userAnswerPulledFromSettings As String
    userAnswerPulledFromSettings = My.MySettings.Default.settings_DbFilePath

    Dim connString = dBprovider & userAnswerPulledFromSettings 'put your connection string together
    Dim myconnection As OleDb.OleDbConnection = New OleDb.OleDbConnection() 'initialize your connection object
    myconnection.ConnectionString = connString 'use your connection string
End Sub

簡而言之

datafile = "C:\Year 13\SheetFormats.accdb"

成為

'Now lets ask the user for the DB path
Dim message, title, defaultValue As String 'initialize some strings
Dim userAnswer As Object 'initialize an object
message = "Please enter the path of your db file"
title = "DB path grabber"
defaultValue = ""
userAnswer = InputBox(message, title, defaultValue) 'This line triggers the prompt
If userAnswer IsNot "" AndAlso File.Exists(userAnswer) Then
    'if the user entered something, and that something is a valid path
    My.MySettings.Default.settings_DbFilePath = userAnswer 'park that value in the user setting
    My.MySettings.Default.Save() 'save the settings
Else
    Return 'the user did not provide a valid path so get out of here without even attempting to connect!
End If
Dim userAnswerPulledFromSettings As String
userAnswerPulledFromSettings = My.MySettings.Default.settings_DbFilePath

為什么不只是添加配置文件。 您可以在安裝應用程序時生成它,然后在需要時讓用戶從那里更改路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM