1

When a form loads, I need to read a binary file under the /skubin folder and use it to populate a List. But I’m unable to open the file. When I do, I receive an error indicating that the file doesn’t exist.

Below is the snippet of my code which I am trying to read the file from the folder.

string startpath = Application.StartupPath;           
string BinDir = Path.Combine(startpath, "skubin");
binNanme = Path.Combine(BinDir, "skuen.bin");
if (!File.Exists(binNanme))
{
    MessageBox.Show("Load bin fail");
    return;
}

When checking the BinDir value, instead of pointing to <project_root>/skubin, it's pointing to <project_root>/bin/Debug/skubin.

I am not understanding why it is pointing to /bin/Debug folder.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77

3 Answers3

1

Right-click on the .bin files in the skubin folder within solution explorer and select properties. Set "Copy to Output Directory" to "Copy Always". This should solve your problem without making any code changes. I am assuming you need those binary files at run-time.

Raju Joseph
  • 493
  • 1
  • 6
  • 13
0

When you compile your project, the results are placed into a {projectFolder}\bin\Debug, and when debugging, the application is run from there.

You have 2 choices:

Keep your code as-is, and in the properties window, mark your bin files as "Copy if newer" or "Copy always". This will copy the files into \bin\Debug\skubin when you compile, and access them from there. This would simulate deploying those files with your application.

-- Or --

Modify your code to move up 2 directories from Application.StartupPath:

string BinDir = Path.Combine(startpath, "..\\..\\skubin");

This would be the option if you're not thinking about deploying your application, and just running it from within your project folder.

0
string filepath = Server.MapPath("~/skubin")
filepath= Path.Combine(filepath, "skuen.bin")
// open the file 

//if not in a controller,  you may use this

HttpContext.Current.Server.MapPath("~/skubin")

For Windows App, you may try Best way to get application folder path

Allen King
  • 2,372
  • 4
  • 34
  • 52