简体   繁体   中英

Open csv file from folder created in application

To start from the beginning:

  1. I have created a Windows Form Application in Visual Studio

  2. I have created a folder named "dataFolder" by adding new folder to project

  3. I have added a new csv file in the folder "dataFile.csv"

I want to use StreamReader to read that file but i have no idea how to get to it.

Now my question is: how to access that file in the folder I just created?

The file path of the folder in the project folder is: "C:\\Users\\user\\Documents\\Visual Studio 2012\\Projects\\AppName\\AppName\\dataFolder"

I have setup the file to be copied on publish.

When I publish the app and I install it the path to that folder changes (obviously). The app path is something like: "C:\\Users\\user\\AppData\\Local\\Apps\\2.0\\X8NNA......."and more numbers and letters.

How do I access that csv file?

Thank you.

在此处输入图片说明 Edit 1: Based on reply

I am doing this:

    string fileData = CSVApplication.Properties.Resources.dataFile;
    string[] lines = fileData.Split(Environment.NewLine.ToCharArray());
    foreach (string line in lines)
        {
            MessageBox.Show(line);
        }

Even if i only have 3 lines in my csv file the code above gives me 5 message boxes. The first message box with the header line, the second empty, the third message box is showing the second line of the csv file, the fourth message box is empty, and the last message box is showing the last row in my csv file.

I do not understand why I am getting those blank lines.

If i can get rid of them then I guess i will have to Split the lines so I can load the data in a Dataset.

If the file is always the same you could create it as a resource so:

Right click the project, go to Resources. From the top select Add a Resource, Add an existing file. Navigate to it. Then you can access the file in code:

string fileData = MyNamespace.Properties.Resources.csvFile
String[] lines = fileData.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToArray();

Another way is to use Environment.CurrentDirectory which will give you the folder your executable is in. You can then figure out the path of your folder from this and modify it accordingly.

For example, assume I wanted to read a file from my resources folder at runtime to get to a file named Thing.csv I could do something like this:

string path = Environment.CurrentDirectory;
string[] parts = path.Split(@"\".ToCharArray()); 
var list = parts.TakeWhile(x=>x != "bin").ToList();
list.Add("Resources");
list.Add("Thing.csv");
var pathStr = string.Join(@"/", list.ToArray());

The above works assuming a folder named Resources exists in the same folder as the bin folder in a typical winforms app. If you deploy via publish then the logic breaks down.

Update: If you wish there to be a folder that exists as part of a deployment then you could add a deployment project to your solution and create the folder within that. Otherwise the folder will need to be created manually after deployment. There are free setup programs as well you could download such as this

Once you know where the folder is relative to your executable you can drop a file in at runtime - read Environment.CurrentDirectory, modify the path in a predictable manner which is most likely by removing any extra folders from the CurrentDirectory path string. Then Append the folder + filename to the path, then use StreamReader.

Ultimately, how you do this is dependent on your app logic of course :) You might choose to read the path to your folder from a configuration file which you can edit post deployment or you could add a function in your forms app to set a directory to look in and persist the info. Lots of options.

Hope this Helps.. You may need some performance optimizations

public List<string> Read ( String csvFilePath )
{
   var list = new List<string>();

   using (var stream = new FileStream( csvFilePath, FileMode.Open))
   {
      var reader = new StreamReader( stream);
      stream.Seek(0, SeekOrigin.Begin); 
       list.AddRange(reader.ReadToEnd().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList());
    }

   return list;
}

Usage Example :

// With this path assigning you' ll get your executable's root directory
var Path = AppDomain.CurrentDomain.BaseDirectory + "\YourFolder";

var csvList = Read(Path);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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