0

I'm fairly new to C#. I've got a small project that makes use of a CSV file. I want the user of the said little app to be able to place the CSV file in the same folder as the startup file (or the .application file) and the program to be able to find it. I tried using the Application.StartupPath but that led me to a different file path than the one I was hoping for.

Currently, when I publish my project, I've got my 'setup.exe', 'Floyds.application' and 'Application Files' all sitting in 'C:\Users\Dell\Desktop\Floyds Published'. This is where I'd ideally like to be able to place my CSV file such that the app reads it. (Of course, this is just an example in my specific case, I'd like the little folder with those three to be the place where you can place CSV files regardless of where it is.)

Alternatively, if anyone can give me another means of getting a simple relative file path that would make is easy enough for someone who doesn't know their way around computers too well to easily dump the file in a folder and for my program to be able to read it, that would also work.

string filepath;
            if(FileAddressBox.Text == "")
            {
                filepath = Application.StartupPath + @"\InitialTable.csv";
            }
            else
            {
                filepath = FileAddressBox.Text;
            }

            using (var reader = new StreamReader(filepath))

Thanks in advance!

Edit: I should have probably clarified that it is a winforms application. My apologies!

Edit 2: I am ideally hoping that the user of the app swaps the CSV file with other CSV files regularly to get different results when running the application, so as to save the user from having to input the different CSVs filepath manually.

Edit 3: The link I was given to the other question does not seem to work either. Added the code where it is used.

  • Does this answer your question? [How can I get the application's path in a .NET console application?](https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application) – Alberto Jan 28 '20 at 11:30
  • The exe path is the default path on startup, so you should be able to just open the file with no path specifier, e.g. "myfile.csv". If you are changing directory before you want to do this, you can save this location early on via Environment.CurrentDirectory. – 500 - Internal Server Error Jan 28 '20 at 11:30
  • Maybe you can use this: `Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)` – Ali Jan 28 '20 at 11:35
  • I think you need this https://stackoverflow.com/questions/3991933/get-path-for-my-exe – plahic Jan 28 '20 at 11:58

0 Answers0