简体   繁体   English

如何在我的 ASP.NET 应用程序中为文件定义正确的路径

[英]How to define correct path for file in my ASP.NET application

Im trying to save file to D:\folder1\folder2\file.txt code for creating a file but it gives me an error because i have root directory at C:\programfiles(x86)\llsExpress I need a path which will work even when i will deploy application Error of the path我试图将文件保存到 D:\folder1\folder2\file.txt代码以创建文件,但它给了我一个错误,因为我的根目录位于 C:\programfiles(x86)\llsExpress 我需要一个甚至可以工作的路径当我将部署应用程序路径错误

First I'd suggest you to try to store your file in a relative path in the root folder if possible.首先,如果可能,我建议您尝试将文件存储在根文件夹中的相对路径中。 This helps with security plus you'll know that all the files for your site are in the root folder.这有助于提高安全性,而且您会知道您网站的所有文件都在根文件夹中。

If you really want to store files on a different drive, you cannot use relative paths to do this.如果您真的想将文件存储在不同的驱动器上,则不能使用相对路径来执行此操作。 So no .. in your paths.所以没有..在你的道路上。 You'll have to use the absolute path.您必须使用绝对路径。 Store that somewhere in config or environment variables to avoid configuring file paths hard coded.将其存储在配置或环境变量中的某个位置,以避免配置硬编码的文件路径。

Finally please use code blocks to show code rather then screenshots on StackOverflow.最后请使用代码块来显示代码,而不是 StackOverflow 上的屏幕截图。 You can do this by putting the code as text between these chars ``.您可以通过将代码作为文本放在这些字符之间来做到这一点``。 like this

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

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