2

In my soloution, for one of the projects I have to add a binary file and read its content in the form_load event.

As you can see in the picture I have added it to the appropriate project and have set the Build Action to Content and Copy to Output Directory as Copy Always.

Now can somone please tell me how how to access this file?

    private void SetupForm_Load(object sender, EventArgs e)
    {
        //Find the path to file then
        //READ THE FILE            
    }

enter image description here

Dumbo
  • 13,555
  • 54
  • 184
  • 288

5 Answers5

1

Now you should find this file in your output directory after building your project. Given the right path to the file, you can access this file with any method you want.

Some methods can help you to get the path to the file:

  • Directory.GetCurrentDirectory();
  • Environment.CurrentDirectory
Dong
  • 61
  • 4
1

I'm not sure exactly what you're trying to do with the file, and that will determine your best approach here. As per the answer here you have a couple of options. To paraphrase:

Community
  • 1
  • 1
Nick
  • 2,285
  • 2
  • 14
  • 26
  • The problem is not how to read the file or deserialize it. The problem is how to find the path to the file. – Dumbo Jul 08 '13 at 09:47
  • Ah, understood. Well by default .Net searches the application folder so you should try 'string path = @"myFileName";' or similar first, to see if you can located the file without specifying a full path. Note: To be specific the Framework looks in the 'bin' folder in this case. Because you've copied the file there, you should be able to locate it. – Nick Jul 08 '13 at 09:52
1

I think the best method was this, so far:

So when I add the file to the project, it will be in the same folder as the exetubale file of project resides. So for getting the path (including the name of exetuable file I had to use Application.ExecutablePath and to remove the file name and have the pure path to the folder I had to use Path.GetDirectoryName() and finally add the filename I wanted to acces to this path, as you can see below:

var path = Path.GetDirectoryName(Application.ExecutablePath) + "\\YourFileName.bin";
Dumbo
  • 13,555
  • 54
  • 184
  • 288
0

The API you call to read the file depends upon the type of file. But the general pattern is like this.

private void SetupForm_Load(object sender, EventArgs e)
{
    System.IO.Stream input = Application.GetResourceStream(new Uri @"/MyApp;component/content.bin", UriKind.Relative)).Stream;
    BinaryReader binaryReader = new BinaryReader(input);
}
Pranav Negandhi
  • 1,594
  • 1
  • 8
  • 17
  • `GetResourceStream` is not available for me, this method cant be find. – Dumbo Jul 08 '13 at 09:48
  • You're probably missing a DLL reference or a using statement. Check out http://msdn.microsoft.com/en-us/library/ms596994(v=vs.95).aspx for a list of dependencies. – Pranav Negandhi Jul 08 '13 at 09:50
0

You can directly readbytes from the file attached as resource.My resource in this example is SampleWordnew document.

byte[] bob = ReadBytesfromResources.Properties.Resources.SampleWordnew ;