Is it possible, from code, to get some project's output path from its vb/csproj alone? I am using .Net framework (4.8). I could not find anything exactly matching what I am looking for. There is https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.evaluation.project.getpropertyvalue?view=msbuild-17-netcore but this isn't available in the .Net framework.
EDIT
I am coding an output builder, which takes targeted assemblies of a solution projects and copy them in a specified location.
The code below uses MSBuild to get the properties of a csproj file specified as a string, and retrieves the current TargetPath. If you create a Console App in .NET Framework 4.8 it will get the output library path of a .NET Framework project.
using Microsoft.Build.Evaluation;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PathGetter
{
internal class Program
{
static void Main(string[] args)
{
string testCsproj = #"C:\Dotnet\WpfControlLibrary1\WpfControlLibrary1\WpfControlLibrary1.csproj";
string result = GetProperty(testCsproj, "TargetPath");
Console.WriteLine(result);
}
public static string GetProperty(string csproj, string propertyName)
{
using (var collection = new ProjectCollection())
{
var project = new Project(csproj, new Dictionary<string, string>(),
null, collection, ProjectLoadSettings.Default);
return project.Properties.Where(p => p.Name == propertyName)
.Select(p => p.EvaluatedValue).SingleOrDefault();
}
}
}
}
Related
I have a project which contains two files: book.xls and book.xlsx. If I run the following code (on .NET Framework) it finds both files as expected, despite only passing .xls as extension.
using System;
using System.IO;
using System.Linq;
namespace GetFilesFromExtensionsWithTests
{
public class Program
{
static void Main(string[] args)
{
var filesWithExtension = FindFiles("../../", "*.xls");
foreach (string file in filesWithExtension)
{
Console.WriteLine($"Found: {file}");
// Found: ../../ book.xls
// Found: ../../ book.xlsx
}
Console.ReadKey();
}
static public string[] FindFiles(string path, string extension)
{
var files = Directory.EnumerateFiles(path, extension).Select(p => p).ToArray();
return files;
}
}
}
This is expected behavior: when you pass a three character extension to Directory.EnumerateFiles it will find all extensions which start with xls (docs):
When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:
If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".
The strange thing is that if I run FindFiles from an xUnit project (.NET Core) it only finds book.xls:
using GetFilesFromExtensionsWithTests;
using Xunit;
namespace GetFilesFromExtensionsWithTests_Tests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
string[] files = Program.FindFiles(
#"..\..\..\..\FileExtensionsWithTests", "*.xls"
);
// Test fails, because it only finds book.xls, but not book.xlsx
Assert.Equal(2, files.Length);
}
}
}
Why is there a difference?
Edit 14 September 2020
It's a known issue https://github.com/dotnet/dotnet-api-docs/issues/4052
It's a known issue which is reported at https://github.com/dotnet/dotnet-api-docs/issues/4052
I have some exe files which has been created using either .net framework 4.5 or .net core 2.1 or .net core 3.1.
I want to get framework name and version information from this DLL using only c# application.
I have written below piece of code which is beneficial and works great with DLL files but not with exe.
var dllInformation = Assembly.LoadFrom(#"D:\\MyProgram.dll");
Console.WriteLine(dllInformation.FullName);
Console.WriteLine(dllInformation.ImageRuntimeVersion);
Console.WriteLine(((TargetFrameworkAttribute)dllInformation.GetCustomAttributes(typeof(TargetFrameworkAttribute)).First()).FrameworkName);
I have also gone through these links but I didn't found them useful for exe files:
information from exe file
Determine .NET Framework version for dll
Please let me know if any suggestions available.
The following program should display the version of the assembly. The program
loads two assemblies during runtime using Assembly.LoadFrom method. 1) is a .NET Fx assembly and 2) is a .NET Core assembly. It loads both and displays the framework version without issues. This project is in github. If you are using the github project, you need to have .NET Core 3.1 installled.
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
namespace net007
{
class Program
{
static void Main(string[] args)
{
//A .net framwwork dll in the same output fodler
//as the current executable
var fxAssembly = Assembly.LoadFrom("fx.console.app.exe");
//A .net core dll in the same output fodler
//as the current executable
var netCoreAssembly = Assembly.LoadFrom("core.console.app.dll");
ShowFrameworkVersion(fxAssembly); //.NETFramework,Version=v4.7.2
ShowFrameworkVersion(netCoreAssembly);//.NETCoreApp,Version = v3.1
}
static void ShowFrameworkVersion(Assembly assembly)
{
var attributes = assembly.CustomAttributes;
foreach (var attribute in attributes)
{
if (attribute.AttributeType == typeof(TargetFrameworkAttribute))
{
var arg = attribute.ConstructorArguments.FirstOrDefault();
if (arg == null)
throw new NullReferenceException("Unable to read framework version");
Console.WriteLine(arg.Value);
}
}
}
}
}
You can use PowerShell to detect for the target framework version:
$path = "C:\your dll here.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).CustomAttributes |
Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } |
Select-Object -ExpandProperty ConstructorArguments |
Select-Object -ExpandProperty value
This is my full class to get an "Name.exe" target framework.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
public class TargetVersionChecker : MarshalByRefObject
{
public string GetTargetedFrameWork(string exePath)
{
Assembly fxAssembly;
try
{
fxAssembly = Assembly.ReflectionOnlyLoadFrom(exePath);
var targetFrameworkAttribute = fxAssembly.GetCustomAttributesData().FirstOrDefault(x => x.AttributeType == typeof(TargetFrameworkAttribute));
return targetFrameworkAttribute?.ConstructorArguments.FirstOrDefault().Value.ToString();
}
catch (Exception ex)
{
// I log here the error but is to specific to our system so I removed it to be more simple code.
return string.Empty;
}
}
}
I cannot understand why I am getting an error (using VS2017) for the code in below related to not finding the class ControlFlowGraph which is supposed to be part of the package Microsoft.CodeAnalysis.FlowAnalysis:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.FlowAnalysis;
namespace CodeAnalysisApp3
{
class Program
{
static async Task Main(string[] args)
{
// Attempt to set the version of MSBuild.
var visualStudioInstances = MSBuildLocator.QueryVisualStudioInstances().ToArray();
var instance = visualStudioInstances[0];
Console.WriteLine($"Using MSBuild at '{instance.MSBuildPath}' to load projects.");
// NOTE: Be sure to register an instance with the MSBuildLocator
// before calling MSBuildWorkspace.Create()
// otherwise, MSBuildWorkspace won't MEF compose.
MSBuildLocator.RegisterInstance(instance);
using (var workspace = MSBuildWorkspace.Create())
{
// Print message for WorkspaceFailed event to help diagnosing project load failures.
workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message);
var solutionPath = args[0];
Console.WriteLine($"Loading solution '{solutionPath}'");
// Attach progress reporter so we print projects as they are loaded.
var solution = await workspace.OpenSolutionAsync(solutionPath, new ConsoleProgressReporter());
Console.WriteLine($"Finished loading solution '{solutionPath}'");
// TODO: Do analysis on the projects in the loaded solution
CSharpParseOptions options = CSharpParseOptions.Default
.WithFeatures(new[] { new KeyValuePair<string, string>("flow-analysis", "") });
var projIds = solution.ProjectIds;
var project = solution.GetProject(projIds[0]);
Compilation compilation = await project.GetCompilationAsync();
if (compilation != null && !string.IsNullOrEmpty(compilation.AssemblyName))
{
var mySyntaxTree = compilation.SyntaxTrees.First();
// get syntax nodes for methods
var methodNodes = from methodDeclaration in mySyntaxTree.GetRoot().DescendantNodes()
.Where(x => x is MethodDeclarationSyntax)
select methodDeclaration;
foreach (MethodDeclarationSyntax node in methodNodes)
{
var model = compilation.GetSemanticModel(node.SyntaxTree);
node.Identifier.ToString();
if (node.SyntaxTree.Options.Features.Any())
{
var graph = ControlFlowGraph.Create(node, model); // CFG is here
}
}
}
}
}
private class ConsoleProgressReporter : IProgress<ProjectLoadProgress>
{
public void Report(ProjectLoadProgress loadProgress)
{
var projectDisplay = Path.GetFileName(loadProgress.FilePath);
if (loadProgress.TargetFramework != null)
{
projectDisplay += $" ({loadProgress.TargetFramework})";
}
Console.WriteLine($"{loadProgress.Operation,-15} {loadProgress.ElapsedTime,-15:m\\:ss\\.fffffff} {projectDisplay}");
}
}
}
}
However, when I compile the above code I am getting the following error message with VS2017:
1>Program.cs(67,41,67,57): error CS0103: The name 'ControlFlowGraph' does not exist in the current context
1>Done building project "CodeAnalysisApp3.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Version used:
Microsoft (R) Visual C# Compiler version 4.8.3761.0
for C# 5
Based on my test, I find I can use class ControlFlowGraph.
I installed the following nugetpackage.
Microsoft.CodeAnalysis
Microsoft.Build.Locator
Then, you will see the following result.
Besides, I used .net framwork 4.6.1.
I was able to solve the problem when I used roslyn CodeAnalysis packages with the proper versions:
CodeAnalysis.CSharp.Workspaces (3.4.0)
CodeAnalysis.FlowAnalysis.Utilities (2.9.6)
CodeAnalysis.Workspaces.MSBuild (3.4.0)
The target framework is .NETFramework 4.7.2
A link to a closed issue created for this question on roslyn Github repo is here
I wanna generate an exe file with some changes in code from another C# exe.
I know that can easy compile .cs single class using CodeDom.Compiler
The thing I want to know is how to compile a project with 'Resources', 'Settings', 'Forms' and other elements.
CSharpCodeProvider.CompileAssemblyFromSource(CompilerParameters, sources[]);
So, the question is where can I add all resources, settings and form (.resx)?
And can I do it with byte[] streams. Without unpacking project's zip.
Sorry for bad English and mby stupid questions. I wish somebody will help me...
For Example: I have byte[] array of resource file 'pic.png' and I wanna attach it to compiled exe as embedded resource.
You should learn about the new compiler service provided by Microsoft in Microsoft.CodeAnalysis code name "Roslyn".
Roslyn provides you the way to compile the code and everything on the fly including creating and compiling complete solution and projects in-memory.
I think what you're looking for can be achieved via Roslyn. See below sample:
class Program
{
static void Main()
{
var syntaxTree = SyntaxTree.ParseCompilationUnit(
#"using System;
using System.Resources;
namespace ResSample
{
class Program
{
static void Main()
{
ResourceManager resMan = new ResourceManager(""ResSample.Res1"", typeof(Program).Assembly);
Console.WriteLine(resMan.GetString(""String1""));
}
}
}");
var comp = Compilation.Create("ResTest.exe")
.AddReferences(new AssemblyNameReference("mscorlib"))
.AddSyntaxTrees(syntaxTree);
var resourcePath = "ResSample.Res1.resources"; //Provide full path to resource file here
var resourceDescription = new ResourceDescription(
resourceName: "ResSample.Res1.resources",
dataProvider: () => File.OpenRead(resourcePath),
isPublic: false);
var emitResult = comp.Emit(
executableStream: File.Create("ResTest.exe"),
manifestResources: new[] { resourceDescription });
Debug.Assert(emitResult.Success);
}
Original Source here
At line dataProvider: () => File.OpenRead(resourcePath), you can provide your own 'FileStream' like () => return _myResourceStream) for your resource file.
After looking all over the Google I found a good way to build a solution. However the solution I want to build also contains unit test projects, which I don't want to include in the build, or if I can't prevent that at least put those binaries in a separate folder. The code is as follows:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
public class BuildSolution
{
private readonly string _solutionPath;
private readonly string _outputPath = "C:\\Temp\\TestBuild\\";
public BuildSolution(string solutionPath, string outputPath = null)
{
if (!string.IsNullOrEmpty(outputPath))
_outputPath = outputPath;
_solutionPath = solutionPath;
Directory.EnumerateFiles(_outputPath, "*", SearchOption.AllDirectories)
.Select(x => new FileInfo(x))
.ToList()
.ForEach(x => x.Delete());
}
public void Build()
{
var pc = new ProjectCollection();
var globalProps = new Dictionary<string, string>()
{
{ ProjectPropertyNames.Configuration, "Debug" },
{ ProjectPropertyNames.OutputPath, _outputPath },
{ ProjectPropertyNames.EnableNuGetPackageRestore, "true"},
};
var targetsToBuild = new[] { "Build" };
var buildRequest = new BuildRequestData(_solutionPath, globalProps, null, targetsToBuild, null);
var buildParams = new BuildParameters(pc);
buildParams.Loggers = new List<ILogger>() { new ConsoleLogger(LoggerVerbosity.Minimal) };
var buildManager = BuildManager.DefaultBuildManager;
buildManager.BeginBuild(buildParams);
var buildSubmission = buildManager.PendBuildRequest(buildRequest);
buildSubmission.ExecuteAsync(BuildCompleted, null);
while (!done)
{
Thread.Sleep(10);
}
buildManager.EndBuild();
Console.WriteLine("OverallResult:{0}", buildSubmission.BuildResult.OverallResult);
}
bool done = false;
private void BuildCompleted(BuildSubmission submission)
{
done = submission.IsCompleted;
}
/// <summary>
/// Unused, but I tried it and it gives me back the correct projects but the build fails because of dependant nuget packages
/// </summary>
/// <param name="path">path of solution</param>
/// <returns></returns>
private IEnumerable<FileInfo> GetFirstLevelProjects(string path)
{
foreach (var dir in Directory.EnumerateDirectories(path))
{
foreach (var file in Directory.EnumerateFiles(dir, "*.csproj"))
{
if (!file.Contains("Test"))
yield return new FileInfo(file);
}
}
}
}
nothing fancy about it. (I'm playing with the idea of making the build async so I can update status...we'll see about that, I might switch it back to sync). One thing I tried was that instead of putting the solution in the build request, I would build the project collection using the first level projects (I use git with sub-modules, so I don't want to build all the non-relevant sub-modules). The problem with that route was that the build would fail because of nuget packages (not sure why or how to get around that). When I build the solution it builds successfully, but my outputPath also includes the test binaries. My end game is that the output can get copied to a specific folder of mine. I wouldn't mind having the test binaries if I knew I could filter ALL the binaries that are in the test projects... So how? What options do I have?
Why Not MsBuild?
The easiest way that I can think of to do this is to use MsBuild to do it.
msbuild C:\myFolder\mySolution.sln /p:Configuration=Release
As for not building the tests, this could now be easily changed from within visual studio
Right Click on Solution > Properties > Configuration Properties (on left side)
From there you could switch to release mode and uncheck the box next to your test projects. This will tell visual studio that when it does a release build it can skip these projects.
Without MsBuild
However, if you wanted to keep building them in the manner that you showed, I would change the assembly name (the dll name) so that you could identify them easily in your favorite scripting language.
Right Click on Project > Application Tab (on left side) > Assembly Name Box
I would call them something like SolutionNameSpace.Tests.ProjectUnderTest.dll.
Then in your build process you can filter out SolutionNameSpace.Tests.*.dll. Just be carefull, if you reference testing libraries they could get copied to your output folder also.