5

I have added a file content.dat in my Visual Studio project at the same location where there is form1.cs. But when I try to read or write to file I am getting this error:

Could not find file '...\bin\Debug\content.dat'.

Why its trying to search it in bin\Debug? Where I need to put this file so that it can be read/write easily later also I need to create installer for this, will the reference path distub again?

xdtTransform
  • 1,986
  • 14
  • 34
coure2011
  • 40,286
  • 83
  • 216
  • 349

3 Answers3

13

Presumably it's because you're running in bin\Debug - that's where the application is being launched.

One option is to set the build action for the file to "Content" so that it will copy it into the output directory along with your executable. EDIT: As Hans as pointed out, you should also set the "Copy to Output Directory" property to "Copy Always" or "Copy if newer".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • indeed, setting any file's build action to Content will copy that file to the output directory. – user29964 Jul 23 '10 at 12:44
  • 4
    No it doesn't, just verified it. "Content" means "should be included in the Setup project". The Copy to Output Directory property is the important one. Set it to "Copy if newer". – Hans Passant Jul 23 '10 at 14:47
  • @Hans: I thought both were relevant. Fixed answer to include this information though. – Jon Skeet Jul 23 '10 at 14:51
1

Mark your content.dat within visual studio and where it says build action select copy always or copy if newer

Jonas B
  • 2,351
  • 2
  • 18
  • 26
1

I know this question is rather old now but I had come across the same issue and found that the fix was to get the current directory with this line:

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;

The second Parent gets rid of the reference to the bin. You can then add the path to the file you've added in the same location and it shouldn't take you to bin/debug.

Jordan1993
  • 864
  • 1
  • 10
  • 28