2017-10-13 67 views
4

的腳本(CSX)的位置在F#這是相當容易預定標識__SOURCE_DIRECTORY__ https://stackoverflow.com/a/4861029/2583080C#腳本文件

但是這個標識符沒有在C#腳本工作(CSX文件或C#互動)。

> __SOURCE_DIRECTORY__

(1,1):錯誤CS0103:名稱 '__SOURCE_DIRECTORY__' 不會在目前情況下

存在獲取當前目錄中的更傳統的方式將不會工作。

  1. Directory.GetCurrentDirectory()

    返回:C:\Users\$USER_NAME$\

  2. new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;

    返回:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\ManagedLanguages\VBCSharp\InteractiveComponents\

回答

3

在C#中,你可以採取的caller information屬性優勢(自C#5/VS2012起可用)。只需要聲明的方法是這樣的:

string GetCurrentFileName([System.Runtime.CompilerServices.CallerFilePath] string fileName = null) 
{ 
    return fileName; 
} 

,並調用它沒有指定可選參數:

string scriptPath = GetCurrentFileName(); // /path/to/your/script.csx