简体   繁体   English

从ASP.NET和C#在桌面上打开文件

[英]Open file on the desktop from ASP.NET and C#

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"

Whoops. 哎呀 Just realized you want to do this via ASP.Net aka through a web browser. 刚意识到您想通过Web浏览器通过ASP.Net来执行此操作。

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. HTML5极大地改善了文件系统访问。 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 关于如何支持拖放文件上传等问题,有很多问题,而且都有很好的答案 。但是,您的JavaScript绝对不应被允许伸出并抓住文件,想象它们的桌面上是否有supersecretPasswords,CreditCardNumbers,AndSocialSecurity.csv

You can use an applet if you really want, but be advised support for those is already be phased out. 如果您确实需要,可以使用applet,但是建议您不要使用它们了。 Source 资源

C# has a constant for the desktop, assuming you know the name of the file... string desktopPath = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); 假设您知道文件名,C#会为桌面提供一个常量。string desktopPath = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); File myFile = new File(desktopPath + "ingredients.txt"); File myFile = new File(desktopPath +“ ingredients.txt”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM