简体   繁体   中英

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

Specifically on the "datafile" line where it has the path of the data base file This is meant to be sent to other students and they probably won't have the same path as me so is there any way to create an alert that allows the user to modify the code to fit their path

Hopefully this can help you get you where you want to go. Once you're comfortable with this code, look into swapping Private dbFilePath As String in favor of a User Setting so that the change can be persisted every time the user uses the application.

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

Meh... here it is using user settings. It's a very basic implementation but it gets the job done for little to no requirements. This way assumes you have a user setting called "settings_DbFilePath". To create such setting...

In Solution Explorer -> Your Solution -> Your Project -> Settings

Name: settings_DbFilePath
Type: String
Scope: User
Value:

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

Here is a third alternative so you can better understand what the other 2 ways are doing, so can pinpoint, where in your code should you implement the methods. In this snippet I basically just refactored and appended some stuff to your existing _2013results_Load method. Though, this third alternative does expect you to have created a user setting called settings_DbFilePath of type string , like described in option #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

In a nutshell

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

became

'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

Why not just add a config file. You can generate it when you install the application, then let the user change the path from there if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM