简体   繁体   中英

Code won't work without a relative file path

This is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;






namespace SDD_Single_Project___Michael_Merjane
{
    public partial class NewUser : Form
    {
        private OleDbConnection connection = new OleDbConnection(); //setting up a private connection 

        public NewUser()
        {
            InitializeComponent();
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\schoolwork\Year 11\SDD\3 SINGLE TASK\SDD Single Project - Michael Merjane\SDD Single Project - Michael Merjane\bin\Persondata.accdb; //PROBLEM IS HERE
Persist Security Info=False;"; // there is no security for finding the location, this is not very safe but for the circumstances it works. In the line above, it is finding the location of the database. This could change due to computer and cause the whole program to not run 

        }

        private void btnBack_Click(object sender, EventArgs e) //all of these mean when button is clicked
        {
            this.Hide(); //hides this page
            MainScreen frm = new MainScreen(); //finds the next screen (the main screen)
            frm.Show(); //shows it
        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {

                  try {
                   connection.Open(); // opens the connection
                   OleDbCommand command = new OleDbCommand(); //names command as a new oledbcommand for further use
                   command.Connection = connection;
                   command.CommandText = "insert into Persondata  ( FirstName,LastName,Address,Suburb,Email,Mobile,Gender,Age) values ( '" + txtFirst.Text + "' , '" + txtLast.Text + "' , '" + txtAddress.Text + "' , '" + txtSuburb.Text + "' , '" + txtEmail.Text + "' , '" + txtMobile.Text + "' , '" + dropGender.Text + "' , '" + dropAge.Text + "') ";
                                           // finds where its going to, finds the columns it is going to fill, finds the text boxes that is going to fill them

                       command.ExecuteNonQuery(); //execute the save
                   MessageBox.Show("Data Saved"); //pretty much shows a box saying everything worked
                   connection.Close(); // closes the connection
                        }
                   catch (Exception ex) //if something has gone wrong a catch will occur
                   { 
                       MessageBox.Show("Error   " + ex);  //show the error
                   } //if there is a error message box will appear informing it 
            }
        }



        }

This is code for an assignment I must give in, the problem is I won't be able to hand it because then the absolute path won't find the file. I need a way to use a relative file path that can change due to a change in location. At the moment, the path (as long is it is) goes to the bin folder within the programs files. So if there was a way to change it so it somehow automatically looks in its own program files to the bin or anywhere else in its own program files that would be great.

try :

 var currDir = System.Environment.CurrentDirectory;

then concatenate the path from there...

Put whatever file you want into the folder your current ptoject is. Then

Directory.GetCurrentDirectory()

will give the current folder you are working on. It will give you the release folder of your project. Store it as a string and use it where ever needed.

Sure. That is pretty basic thing - I'd suggest putting database file in DB folder inside bin - or left inside bin as it is.

Then you need to determine location of your binary folder - there are several ways, while two below are most common:

  • Environment.CurrentDirectory - will work until you are not changing it during run-time (elsewhere)
  • Assembly.GetEntryAssembly().Location - which is full path to the executable that started current process

I'd suggest then looking onto System.IO.Path class - first to strip only path from Location and then to combine it back but this time with database file-name string .

While this is you assignment I'm going to leave you here to study this class by yourself - this is pretty interesting one :P

http://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx

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