C# 如何获取csc.exe路径?
有没有办法获取最新.NET Framework的csc.exe的路径 文件通常位于:c:\Windows\Microsoft.NET\Framework\vX.X.XXX中,但问题是可能安装了多个版本+有32位和64位版本C# 如何获取csc.exe路径?,c#,.net,csc,C#,.net,Csc,有没有办法获取最新.NET Framework的csc.exe的路径 文件通常位于:c:\Windows\Microsoft.NET\Framework\vX.X.XXX中,但问题是可能安装了多个版本+有32位和64位版本 此问题的任何解决方案?c:\Windows\Microsoft.NET\Framework\vX.X.XXX应包含最新的32位版本的csc.exe c:\Windows\Microsoft.NET\Framework64\vX.X.XXX应包含csc.exe的最新64位版本
此问题的任何解决方案?c:\Windows\Microsoft.NET\Framework\vX.X.XXX应包含最新的32位版本的csc.exe
c:\Windows\Microsoft.NET\Framework64\vX.X.XXX应包含csc.exe的最新64位版本 不管怎么说,这就是我的
顺便说一句:您可以从程序文件中的VisualStudioTools文件夹中使用VisualStudio命令行访问这两个文件。它会自动设置使用csc编译器构建32位和64位应用程序所需的所有路径。如果您已经安装了Visual Studio,只需: 单击开始,指向所有程序,指向Microsoft Visual Studio,指向Visual Studio工具,然后单击Visual Studio命令提示符 这里有一个命令行框,您可以在其中编译如下:
csc PathToYourCsSource
您可以使用
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
更新:
打开命令提示符或Powershell,然后运行下面的命令,列出不同.Net Framework版本的编译器的完整路径
dir%WINDIR%\Microsoft.NET\Framework64\csc.exe/s/b
CSC路径如下所示:
csc PathToYourCsSource
C:\Program Files\MSBuild\\Bin
例如:如果您使用的是Visual Studio 2013,它将是12.0。查找CSC.exe路径的最佳方法是在CLI(命令行解释器)中运行以下简单行:
dir /s %WINDIR%\CSC.EXE
目录-显示目录
/s-包括子文件夹
%WINDIR%\CSC.EXE-在根文件夹中查找类似“CSC.EXE”的短语
这是我们的结果:
然后我们可以简单地按行编译示例代码,如:
C:\WINDOWS\...\v.4.0.30319\CSC.exe HelloWorld.cs
尊敬。微软最近已经记录了这一点-检查 csc.exe可执行文件通常位于 Windows目录下的Microsoft.NET\Framework\文件夹。 它的位置可能会因服务器的确切配置而异 特别是计算机。如果安装了多个.NET Framework版本 安装在您的计算机上,您将发现此的多个版本 文件
我假设您想使用
csc.exe
在一台普通的非开发人员PC上编译文件,,这就是您询问路径的原因
您可以使用我创建的以下小批量脚本来获取可用的最新csc.exe的路径,而不是为此目的查看注册表路径在脚本的第一部分确定,然后存储在环境变量dotNet
中:
@echo off
pushd .& setlocal
set dotNetBase=%SystemRoot%\Microsoft.NET\Framework\
rem get latest .net path containing csc.exe:
set dotNet20=%dotNetBase%v2.0.50727\
set dotNet35=%dotNetBase%v3.5\
set dotNet40=%dotNetBase%v4.0.30319\
if exist %dotNet20%nul set dotNet=%dotNet20%
if exist %dotNet35%nul set dotNet=%dotNet35%
if exist %dotNet40%nul set dotNet=%dotNet40%
set msbuildDir=%ProgramFiles(x86)%\MSBuild\14.0\Bin\
set cscExe="%msbuildDir%csc.exe"
if not exist %cscExe% set cscExe="%dotNet%csc.exe"
::echo %cscExe%
set assemblies=
REM reference required assemblies here, copy them to the output directory after the colon (:), separated by comma
REM set assemblies=-reference:.\PresentationCore.Dll,.\WindowsBase.dll
set runArgs=%3 %4 %5 %6 %7 %8 %9
if not "%1"=="/run" (set outPath=%~DP1& set outFileName=%~n1& CALL :Compile) else (set outPath=%~DP2& set outFileName=%~n2& CALL :CompileAndRun)
GOTO :End
:Compile
echo Compiling "%outPath%%outFileName%.cs"
if exist "%outPath%%outFileName%.exe" del /F "%outPath%%outFileName%.exe"
%cscExe% -optimize+ -lib:"%dotNet%\" %assemblies% /t:exe /out:"%outPath%%outFileName%.exe" %outFileName%.cs
exit /B
:CompileAndRun
echo Compiling "%outPath%%outFileName%.cs"
if exist "%outPath%%outFileName%.exe" del /F "%outPath%%outFileName%.exe"
%cscExe% -optimize+ -lib:"%dotNet%\" %assemblies% /t:exe /out:"%outPath%%outFileName%.exe" %outFileName%.cs >nul
echo Running "%outPath%%outFileName%.exe"
"%outPath%%outFileName%.exe" %runArgs%
exit /B
:End
::::
然后,它将使用该路径调用嵌入Windows的最新可用C#编译器。请注意,VisualStudio使用的C#编译器比Windows更新,Windows仅限于C#version 5。当您打算用它编译源代码时,请记住这一点
我已经更新了代码,以便在安装时可以使用
将其另存为CompileCS.cmd,并将其放在与*.cs文件相同的路径中。然后,您可以简单地按如下方式编译它:
csc PathToYourCsSource
编译GetDotNetVersion.cs
它将编译控制台应用程序GetDotNetVersion,这是一个确定已安装的.NET版本的程序,我已经发布了
提示:如果要在编译后立即运行C#应用程序,请使用/run
参数,如下所示(如果需要,可以在.cs文件末尾传递命令行参数,这些参数将传递到编译后的.exe文件):
编译ECS/运行GetDotNetVersion.cs
[arg1]。。。[arg7]
该脚本检查.NET 2.0.x、3.5和4.0.30319的系统目录是否存在-在我从未见过的其他文件夹中csc.exe。因为它从最旧版本到最新版本进行检查,所以变量dotNet
包含最新的现有路径
请注意
- Microsoft将.NET的所有4.x版本(包括最新版本4.7.1)存储在文件夹4.0.30319中。因此,如果安装了.NET4.x的任何版本,您都会在那里找到它
- 如果需要64位版本而不是32位版本,只需在环境变量
中用dotnebase
替换Framework64
(脚本的第二行)。请注意,即使在64位Windows上,通常也会安装32位版本的框架,如果您真的不需要64位,那么它在大多数32位的PC上也可以正常工作。但是,如果您愿意,您可以添加一个检查路径是否存在的选项,并选择Framework64(就像我为.NET版本所做的那样)Framework
- 如前所述,虽然
仍然存在,但它仅限于C#version 5(每次调用csc.exe时都会收到此警告)。但是对于许多没有什么用处的控制台应用程序来说,C#5仍然很好。如果需要更高版本,则需要Visual Studio或访问以获取Roslyn编译器源代码csc.exe
- 如果正在引用外部程序集(*.dll),
设置程序集=-reference:。\PresentationCore.dll、。\WindowsBase.dll
-只需附加所需的任何dll-列表以逗号分隔。您需要将它们从dotNet
路径复制到输出目录(创建*.exe的地方)。如果不需要程序集,只需将其设置为空(即set assemblies=
)
protected override System.CodeDom.Compiler.CompilerResults FromFileBatch(System.CodeDom.Compiler.CompilerParameters options, string[] fileNames)
{
#if NETSTANDARD2_0
return NetStandardFromFileBatch(options, fileNames);
#else
return OldFromFileBatch(options, fileNames);
#endif
}
#if NETSTANDARD2_0
protected System.CodeDom.Compiler.CompilerResults NetStandardFromFileBatch(System.CodeDom.Compiler.CompilerParameters options, string[] fileNames)
{
//// C:\Program Files\dotnet\sdk\2.0.0\Roslyn
//string sysver = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion();
//System.Console.WriteLine(sysver);
//string pf64 = System.Environment.ExpandEnvironmentVariables("%ProgramW6432%");
//string pf32 = System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%");
//string pf = pf32;
//if (System.IntPtr.Size * 8 == 64)
// pf = pf64;
//// compilerDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);
////compilerDirectory = System.IO.Path.Combine(compilerDirectory, "dotnet", "sdk", "2.0.0", "Roslyn");
//compilerDirectory = System.IO.Path.Combine(pf32, "MSBuild", "14.0", "Bin");
//if (System.IntPtr.Size * 8 == 64)
// compilerDirectory = System.IO.Path.Combine(compilerDirectory, "amd64");
string assemblyName = System.IO.Path.GetFileNameWithoutExtension(options.OutputAssembly);
Microsoft.CodeAnalysis.SyntaxTree[] syntaxTrees = new Microsoft.CodeAnalysis.SyntaxTree[fileNames.Length];
for (int i = 0; i < fileNames.Length; ++i)
{
string fileContent = System.IO.File.ReadAllText(fileNames[i], System.Text.Encoding.UTF8);
Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions op = null;
// ERR_EncodinglessSyntaxTree = 37236 - Encoding must be specified...
syntaxTrees[i] = Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(
fileContent, op, fileNames[i], System.Text.Encoding.UTF8
);
}
Microsoft.CodeAnalysis.MetadataReference[] references =
new Microsoft.CodeAnalysis.MetadataReference[options.ReferencedAssemblies.Count];
for (int i = 0; i < references.Length; ++i)
{
references[i] = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(
options.ReferencedAssemblies[i]
);
}
Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilationOptions co =
new Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilationOptions
(
Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary
);
co.WithOptionStrict(Microsoft.CodeAnalysis.VisualBasic.OptionStrict.Off);
co.WithOptionExplicit(false);
co.WithOptionInfer(true);
Microsoft.CodeAnalysis.Compilation compilation = Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation.Create(
assemblyName,
syntaxTrees,
references,
co
);
System.CodeDom.Compiler.CompilerResults compilerResults = new System.CodeDom.Compiler.CompilerResults(options.TempFiles);
compilerResults.NativeCompilerReturnValue = -1;
// using (var dllStream = new System.IO.MemoryStream())
using (System.IO.FileStream dllStream = System.IO.File.Create(options.OutputAssembly))
{
using (System.IO.MemoryStream pdbStream = new System.IO.MemoryStream())
{
Microsoft.CodeAnalysis.Emit.EmitResult emitResult = compilation.Emit(dllStream, pdbStream);
if (!emitResult.Success)
{
foreach (Microsoft.CodeAnalysis.Diagnostic diagnostic in emitResult.Diagnostics)
{
// options.TreatWarningsAsErrors
if (diagnostic.IsWarningAsError || diagnostic.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
{
string errorNumber = diagnostic.Id;
string errorMessage = diagnostic.GetMessage();
string message = $"{errorNumber}: {errorMessage};";
string fileName = diagnostic.Location.SourceTree.FilePath;
Microsoft.CodeAnalysis.FileLinePositionSpan lineSpan = diagnostic.Location.GetLineSpan();
string codeInQuestion = lineSpan.Path;
int line = lineSpan.StartLinePosition.Line;
int col = lineSpan.StartLinePosition.Character;
compilerResults.Errors.Add(
new System.CodeDom.Compiler.CompilerError(fileName, line, col, errorNumber, errorMessage)
);
} // End if
} // Next diagnostic
// emitResult.Diagnostics
// CheckCompilationResult(emitResult);
}
else
{
compilerResults.PathToAssembly = options.OutputAssembly;
compilerResults.NativeCompilerReturnValue = 0;
}
}
}
// compilerResults.CompiledAssembly = System.Reflection.Assembly.Load(array3, null);
return compilerResults;
}
#endif
where csc
C:\Program Files\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe