ASP.NET: Path.comBine مع المسارات النسبية
-
18-09-2019 - |
سؤال
أحاول تحويل "~ / images / images /" إلى مسار مطلق يمكنني إنشاء FileStream من. لقد جربت VictHathutility و Path.comBine ولكن لا شيء يبدو أنه يعطيني المسار الصحيح. الأقرب الذي حصلت عليه هو Virtualathutility. التوظيف، لكن ذلك كان مجرد موقع الملف كطفل مباشر من ج:.
يجب أن يكون هناك طريقة للقيام بذلك.
المحلول
أنت تبحث عن MapPath
طريقة.
// get the path in the local file system that corresponds to ~/Uploads/Images
string localPath = HttpContext.Current.Server.MapPath("~/Uploads/Images/");
استخدمها جنبا إلى جنب مع Path.comBine لإنشاء مسار الملف:
string fileName = Path.Combine(
HttpContext.Current.Server.MapPath("~/Uploads/Images/"),
"filename.ext");
using (FileStream stream = File.OpenRead(fileName))
{
// read the file
}
لا تنتمي إلى StackOverflow