user3615887
user3615887

Reputation:

Open a file without dialog

with openFileDialog you would select a file and after pressing "OPEN" it would paste the filepath of the selected file (c:\blob\template) into an textbox.

I would like to do automatically select the file c:\blob\template en then put the filepath in the textbox. basically the exact same thing as openfiledialog without a dialog. i have been trying to do this for some time now. can somebody help me out with this? i have no clue how to realise this.

i`m only able to get the filepath and paste the string in the textbox but this but only fills the box with a string. i need to load the file/template in there.

  private void txt()
    {
        string fileName = "template";
        string fullPath;           
        fullPath = Path.GetFullPath(fileName);
        lblFirstTemplate.Text = fullPath;

    }

Thank you in advance!

Upvotes: 0

Views: 2422

Answers (1)

Wouter de Kort
Wouter de Kort

Reputation: 39898

The code you now have will only get the file path. What you need to add is code that will actually open the file and read its content.

Let's say your file contains some text. You can use the following line to read the complete file as text:

System.IO.File.ReadAllText(fullPath);

If your file contains some other data like binary data, you can use:

System.IO.File.ReadAllBytes(fullPath);

And instead of reading all data at once, you can read it one line or a couple of bytes at a time. A good place in the documentation to start is: Common I/O Tasks

Upvotes: 2

Related Questions