2015-06-25 3 views
2

Как я могу получить физический файл, а не путь, связанный с доменом, в моем модульном тесте?Получить физический путь к файлу в модульном тесте, запущенном в домене, вместо относительного пути к домену

Я использую этот код:

var codebase = Assembly.GetExecutingAssembly().CodeBase; 
var pathUrlToDllDirectory = Path.GetDirectoryName(codebase); 
var pathToDllDirectory = new Uri(pathUrlToDllDirectory).LocalPath; 

Что я получаю это

файл: \ compdata \ папки $ \ ПользовательА \ Documents \ Projects \ ProjectA \ ProjectA.Test \ Bin \ Debug

Но то, что я ожидал, это Somthing так:

C:\windows\userA\Documents\Projects\ProjectA\ProjectA.Test\bin\Debug 

ответ

0

Я создал эти расширения для Assembly, чтобы получить их. См., Если это сработает для вас:

using System.IO; 
using System.Reflection; 


public static class AssemblyExtensions 
{ 
    /// <summary> 
    /// Implemented - Returns the full path of the assembly file. 
    /// </summary> 
    public static string GetAssemblyPath(this Assembly Assemb) 
    { 
     string FileName = Assemb.CodeBase; 

     if (FileName.Substring(0, 4).ToUpperInvariant() == "FILE") 
      FileName = FileName.Remove(0, 8); 

     return FileName; 
    } 


    /// <summary> 
    /// Implemented - Returns the path to the folder where the assembly file is located 
    /// </summary> 
    public static string GetAssemblyFolder(this Assembly Assemb) 
    { 
     return Path.GetDirectoryName(GetAssemblyPath(Assemb)); 
    } 


    /// <summary> 
    /// Implemented - Combines the assembly folder with the passed filename, returning the full path of that file in the assmebly's folder. 
    /// </summary> 
    public static string GetFileInAssemblyFolder(this Assembly Assemb, string FileName) 
    { 
     return Path.Combine(GetAssemblyFolder(Assemb), FileName); 
    } 



    /// <summary> 
    /// Implemented - Returns the full name of the embedded resources containing the passed string - Match case 
    /// </summary> 
    /// <param name="Assemb"></param> 
    /// <param name="ResourceName"></param> 
    /// <returns></returns> 
    public static IEnumerable<string> GetResourcesContainingString(this Assembly Assemb, string ResourceName) 
    { 
     return Assemb.GetManifestResourceNames().Where(S => S.Contains(ResourceName)); 
    } 



    /// <summary> 
    /// Implemented - Returns the full name of the first embedded resource containing the passed string - Match case 
    /// </summary> 
    /// <param name="Assemb"></param> 
    /// <param name="ResourceName"></param> 
    /// <returns></returns> 
    public static string GetFirstResourceContainingString(this Assembly Assemb, string ResourceName) 
    { 
     return Assemb.GetResourcesContainingString(ResourceName).First(); 
    } 
} 
Смежные вопросы