سؤال

أحاول تحويل "~ / 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
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top