代码获胜';没有相对文件路径时无法工作

本文关键字:路径 文件 工作 相对 获胜 代码 | 更新日期: 2023-09-27 18:20:21

这是我的代码

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或自己程序文件中的任何其他位置,那就太好了。

代码获胜';没有相对文件路径时无法工作

将您想要的任何文件放入当前ptoject所在的文件夹中。然后

Directory.GetCurrentDirectory()

将提供您正在处理的当前文件夹。它将提供您项目的发布文件夹。将其存储为字符串,并在需要的地方使用。

try:

 var currDir = System.Environment.CurrentDirectory;

然后从那里连接路径。。。

当然。这是非常基本的事情——我建议把数据库文件放在DB文件夹中的垃圾桶里——或者放在垃圾桶里

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

  • Environment.CurrentDirectory-将工作,直到您在运行时不更改它(其他地方)
  • Assembly.GetEntryAssembly().Location-启动当前进程的可执行文件的完整路径

然后我建议查看System.IO.Path类——首先从Location中只剥离路径,然后将其组合回来,但这次使用数据库文件名string

虽然这是你的作业,但我要把你留在这里,让你自己学习这门课——这是一个非常有趣的作业:p

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