简体   繁体   中英

How to detect OS my Asp.Net Core app is running on?

I have an Asp.Net Core 1.0.0 app that runs on both Linux and Windows. In the app I need to upload files and save them in wwwroot subfolders. I get IHostingEnvironment.WebRootPath and specify a subfolder. It works fine on Windows but not on Linux because of paths. Window's \\ of course is not going to work. So how can I detect whether my app is running on Windows or Linux from controller and choose an appropriate path delimiter?

Rather than this, you should use Path.Combine to generate directory names. For example:

var root = env.WebRootPath;
var myDirectory = Path.Combine(root, "subdirectory");

您可以在Windows和Linux上使用/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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