Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码赢得';如果没有相对文件路径,则无法工作_C#_Visual Studio 2012_Relative Path - Fatal编程技术网

C# 代码赢得';如果没有相对文件路径,则无法工作

C# 代码赢得';如果没有相对文件路径,则无法工作,c#,visual-studio-2012,relative-path,C#,Visual Studio 2012,Relative Path,这是我的密码 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;

这是我的密码

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,或者在自己的程序文件中的任何其他位置,这将非常好。

尝试:

 var currDir = System.Environment.CurrentDirectory;

然后从那里连接路径

将所需文件放入当前对象所在的文件夹中。然后

Directory.GetCurrentDirectory()
将提供您正在处理的当前文件夹。它将为您提供项目的发布文件夹。将其存储为字符串并在需要时使用。

当然。 这是一件非常基本的事情——我建议将数据库文件放在bin中的DB文件夹中,或者按原样放在bin中

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

  • Environment.CurrentDirectory
    -将一直工作,直到您在运行时(其他地方)不更改它为止
  • Assembly.GetEntryAssembly().Location
    -它是启动当前进程的可执行文件的完整路径
我建议接下来查看
System.IO.Path
类-首先从
位置
中仅删除路径,然后将其合并回来,但这次是使用数据库文件名
字符串

这是你的作业,我将把你留在这里,让你自己学习这门课——这门课很有趣:p