2014-09-02 34 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