هل هناك طريقة لتحويل برنامج .NET Framework محددات الملف إلى مسارات مع أحرف محركات الأقراص؟

StackOverflow https://stackoverflow.com/questions/278761

  •  07-07-2019
  •  | 
  •  

سؤال

وكنت أبحث عن شيء مثل Server.MapPath في مجال ASP.NET لتحويل إخراج Assembly.GetExecutingAssembly (). تعليمات البرمجة الأساسية في مسار ملف مع حرف محرك الأقراص.

والتعليمة البرمجية التالية يعمل لحالات الاختبار حاولت:

private static string ConvertUriToPath(string fileName)
{
    fileName = fileName.Replace("file:///", "");
    fileName = fileName.Replace("/", "\\");
    return fileName;
}

ويبدو أن يجب أن يكون هناك شيء في .NET Framework التي من شأنها أن تكون أفضل بكثير - أنا فقط لم تكن قادرة على العثور عليه

هل كانت مفيدة؟

المحلول

وحاول تبحث في Uri.LocalPath الممتلكات.

private static string ConvertUriToPath(string fileName)
{
   Uri uri = new Uri(fileName);
   return uri.LocalPath;

   // Some people have indicated that uri.LocalPath doesn't 
   // always return the corret path. If that's the case, use
   // the following line:
   // return uri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top