2014-09-02 29 views
-1

这是我的代码代码不会没有相对文件路径工作

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 
      } 
     } 



     } 

这是一个任务,我必须给代码,问题是我不能够把它因为那样的话绝对路径不会找到该文件。我需要一种方法来使用由于位置更改而可能更改的相对文件路径。目前,路径(只要是这样)进入程序文件中的bin文件夹。所以如果有一种方法来改变它,所以它会自动查找它自己的程序文件到bin或者它自己的程序文件中的任何其他地方,这很好。

回答

1

把你当前ptoject是文件夹。然后

Directory.GetCurrentDirectory() 

将给你正在工作的当前文件夹。它会为您提供项目的发布文件夹。将其存储为一个字符串并在需要时使用它。

1

尝试:

var currDir = System.Environment.CurrentDirectory; 

然后从那里串连路径......任何你想要的文件

0

当然。 这是非常基本的东西 - 我建议将数据库文件放在bin文件夹中的DB文件夹中 - 或者保留在bin文件夹中。

然后,你需要确定你的二进制文件夹的位置 - 有几种方法,而下面两个是最常见的:

  • Environment.CurrentDirectory - 会工作,直到你是不是在运行时改变它(其他地方)
  • Assembly.GetEntryAssembly().Location - 这是完整路径的可执行文件,启动电流过程

我建议再寻找到System.IO.Path类 - 先剥去唯一路径10,然后将其组合回来,但是这次使用数据库文件名string

虽然这是你任务,我要离开这里,你自己去研究这个阶级 - 这是很有趣的一个:P

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