【问题标题】:Code won't work without a relative file path没有相对文件路径,代码将无法工作
【发布时间】:2014-09-02 23:46:10
【问题描述】:

这是我的代码

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

【问题讨论】:

    标签: c# visual-studio-2012 relative-path


    【解决方案1】:

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

    Directory.GetCurrentDirectory()
    

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

    【讨论】:

      【解决方案2】:

      试试:

       var currDir = System.Environment.CurrentDirectory;
      

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

      【讨论】:

        【解决方案3】:

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

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

        • 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

        【讨论】:

          猜你喜欢
          • 2017-06-29
          • 1970-01-01
          • 2018-06-04
          • 1970-01-01
          • 1970-01-01
          • 2020-05-08
          • 2019-09-07
          • 2019-02-17
          • 1970-01-01
          相关资源
          最近更新 更多