简体   繁体   English

在控制台应用程序中访问文件夹

[英]Accessing folder in console application

I'm trying to write a function to read csv contents into a datatable. 我正在尝试编写一个将csv内容读入数据表的函数。
I'm getting an exception about the file path and wondering what it is that I'm doing wrong. 我在文件路径方面遇到异常,想知道我做错了什么。 All I did was create the console app and create a folder in the project called 'Data'. 我所做的就是创建控制台应用程序,并在项目中创建一个名为“ Data”的文件夹。

public DataTable ReadCSV(string filename)
{
        DataTable dt = new DataTable();
        string sql = "SELECT * FROM " + filename;
        string path = "Data\\";
        string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + filename + ";" + "Extended Properties='text;FMT=Delimited(;);HDR=YES'";
        OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connstring);
        System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);

        try
        {
            conn.Open();
            da.Fill(dt);
        }
        catch (Exception ex)
        {
            Console.WriteLine(filename + "not found");
        }

        finally
        {
            conn.Close();
        }
        return dt;
    }

}

My connection string in the Text visualizer when I run in debug mode: 在调试模式下运行时,我在Text可视化工具中的连接字符串:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data\\Positions.csv;Extended Properties='text;FMT=Delimited(;);HDR=YES' Provider = Microsoft.Jet.OLEDB.4.0;数据源= Data \\ Positions.csv;扩展属性='文本; FMT =定界(;); HDR =是'

I'm getting an exception 我要例外了

base {System.Data.Common.DbException} = {"'Data\\Positions.csv' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides."} “ base {System.Data.Common.DbException} = {”'Data \\ Positions.csv'不是有效的路径。请确保路径名称拼写正确,并且已连接到文件所在的服务器。“ }

Can anyone point me in the right direction? 谁能指出我正确的方向? I have limited experience doing console apps so it's probably some formatting mistake that I've made. 我做控制台应用程序的经验有限,因此可能是我犯了一些格式错误。 Thanks 谢谢

and create a folder in the project called 'Data'. 并在项目中创建一个名为“数据”的文件夹。

That doesn't work. 那不行 Your program is running in the bin\\Debug subdirectory of your project. 您的程序正在项目的bin \\ Debug子目录中运行。 It doesn't have a Data subdirectory. 它没有数据子目录。 You'd have to use ..\\..\\Data\\Positions.csv to find that file. 您必须使用..\\..\\Data\\Positions.csv来找到该文件。

Well, that would solve your problem right now but it isn't going to be useful once you copy your program to another machine. 好吧,这将立即解决您的问题,但是一旦将程序复制到另一台计算机上,它将不再有用。 There won't be a ..\\..\\Data directory there. 那里没有..\\..\\Data目录。 Think about ways that your user is going to tell you where the .csv file is located. 想一想您的用户将如何告诉您.csv文件的位置。 A GUI with OpenFileDialog is the friendly way but not very compatible with a console app. 带有OpenFileDialog的GUI是一种友好的方式,但与控制台应用程序不太兼容。 The standard way for that is to pass command line arguments. 这样做的标准方法是传递命令行参数。 Environment.CommandLine. Environment.CommandLine。 Not very compatible with the typical user. 与典型用户不太兼容。 You'll have to weigh these options by yourself. 您必须自己权衡这些选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM