0

I want a simple solution to open a file on desktop. Below is the file path that I would like to open. I don't want to use browse button to upload the file. I just want that file to load up as a textfile and be read.

"C:\Users\Donald\Desktop\ingredients.txt"
Deepak Arora
  • 440
  • 2
  • 11
D B
  • 127
  • 1
  • 12
  • Read by whom? The user? – Barry O'Kane Apr 29 '16 at 15:08
  • Yes, so me being a user. I have that file on the desktop, i want to open it without having to browse through a dialogue or go to the desktop myself. It just locates it and opens. – D B Apr 29 '16 at 15:09
  • 1
    `Process.Start("C:\Users\Donald\Desktop\ingredients.txt");` will open it in the default application. – Barry O'Kane Apr 29 '16 at 15:12
  • 2
    Your tags mention ASP.net do you mean you want to open the file from within a browser application ? I.e. in JavaScript ? – PhillipH Apr 29 '16 at 15:14
  • Please correct this if I am wrong but you want the user to be abel to use the web browser (your asp.net app) to find the file with (button click or something) and process it with javascript or send it to the server via a post. If I am wrong and you want to open it directly in the server then you can't. The server should (and cant) know anything about a users desktop. – Igor Apr 29 '16 at 15:16
  • @Igor well basically i have created a txt file which gets created on a users desktop. This is done by themselves locating an area to save to. I want to then open it once its saved. – D B Apr 29 '16 at 15:18
  • Please take note of my updated answer @DonaldBury – TheNorthWes Apr 29 '16 at 15:26

1 Answers1

6

Whoops. Just realized you want to do this via ASP.Net aka through a web browser.

You cannot do that. Because this would be an enormous security risk if any old web page could reach in and grab files. HTML5 has dramatically improved file system access though. There are many questions, with good answers, on how to support drag and drop file upload etc. But your javascript should never be allowed to reach out and grab files, imagine if their desktop had supersecretPasswords,CreditCardNumbers,AndSocialSecurity.csv

You can use an applet if you really want, but be advised support for those is already be phased out. Source

C# has a constant for the desktop, assuming you know the name of the file... string desktopPath = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); File myFile = new File(desktopPath + "ingredients.txt");

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35