1

I have a windows form application which works with text file data sets that I tried to make portable (ie: To run the application from external hard drive or pen drive does not need to copy the data sets into the C:\ drive directly.

I changed

StreamReader fileitem = new StreamReader("c:\\dataset.txt");

into

StreamReader fileitem = new StreamReader("dataset.txt");

and copy the dataset into the exe file path (.../bin/debug) But it shows an error "function has stopped working"! Any idea?

anaximander
  • 7,083
  • 3
  • 44
  • 62
user3403967
  • 53
  • 1
  • 6

1 Answers1

1

Here's a sample of how you can get the absolute path to your executable file:

static public string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return Path.GetDirectoryName(path);
    }
}

Sample taken from this answer

If you implement this property you can then update your code to the following:

StreamReader fileitem = new StreamReader(AssemblyDirectory + "dataset.txt");
Community
  • 1
  • 1
Logarr
  • 2,120
  • 1
  • 17
  • 29
  • Thanks @Logarr. But when I run it. I get the same error. – user3403967 Mar 11 '14 at 19:52
  • I'll need more specifics then. How large are your data files? Are you still trying to run this from within `bin/debug`? Have you set a breakpoint and stepped into the code to confirm the problem is with this line of code? – Logarr Mar 11 '14 at 20:11
  • The data sets are around 2.5 GiG. Yeap, I am still trying to do it within the same path @Logarr – user3403967 Mar 11 '14 at 21:28
  • And that's the same 2.5 GB file that worked when you put it directly on the C: drive? – Logarr Mar 11 '14 at 21:33
  • @user3403967 - Sorry I haven't gotten back to you. Did you solve the problem? – Logarr Mar 13 '14 at 17:42