[英]Build a Visual Studio Solution with C# AND Powershell projects
Visual Studio 2015 Visual Studio 2015
VS newb. VS newb。 I have many PS scripts that I want to migrate into a C# Solution.
我有很多PS脚本要迁移到C#解决方案中。 The solution will in theory do stuff and then with button presses or other triggers, run the Powrshell scripts.
从理论上讲,该解决方案将完成工作,然后通过按下按钮或其他触发器来运行Powrshell脚本。
The problem is that when I add the two projects, I don't know how to access the PS script from the C# side. 问题是,当我添加两个项目时,我不知道如何从C#端访问PS脚本。 I tried adding the PS Project to the Solution then adding a Reference to it.
我尝试将PS项目添加到解决方案中,然后添加对它的引用。 But when I try to build (even with a blank PS Script) I get error "CS0006 Metadata file 'MyPath\\PSTest\\bin\\Debug\\MyApplication.exe could not be found"
但是,当我尝试构建(即使使用空白PS脚本)时,也会收到错误消息“找不到CS0006元数据文件'MyPath \\ PSTest \\ bin \\ Debug \\ MyApplication.exe”
Is it possible to even do this? 有可能做到这一点吗? All I want is a Solution that has a main C# project and a Powershell project included that I can edit and use those scripts from the C# side.
我想要的是一个包含一个主要C#项目和一个Powershell项目的解决方案,我可以从C#端编辑和使用这些脚本。
EDIT: Do not really want to just invoke some scripts from within the C# environment. 编辑:真的不想只从C#环境中调用一些脚本。 I want to add a Powershell Project to the Solution and further develop the Powershell scripts from there as well as create the new app in C# that will use the Powershell Project.
我想将Powershell项目添加到解决方案中,然后从那里进一步开发Powershell脚本,并在C#中创建将使用Powershell项目的新应用程序。
I'll offer a couple of similar options. 我将提供几个类似的选项。
Option 1, similar to GibralterTop's answer, is to put the PS Scripts in your C# project. 选项1与GibralterTop的答案类似,是将PS脚本放入您的C#项目中。
When your program runs, it will be able to find the scripts in 当程序运行时,它将能够在其中找到脚本
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts")
Option 2 is similar, but you can keep your PowerShell Script project. 选项2与之类似,但是您可以保留PowerShell脚本项目。
You will need to repeat the steps to add links if you add any new PS scripts. 如果添加任何新的PS脚本,则将需要重复这些步骤来添加链接。
Eventually, you will probably build an installer. 最终,您可能会构建一个安装程序。 You will need to have it put the scripts in the same relative location;
您将需要将脚本放置在相同的相对位置。 a "Scripts" folder under the installation folder.
安装文件夹下的“脚本”文件夹。
If you right click on your C# Project from within VS, in the context menu you will see "Add". 如果从VS中右键单击C#项目,则在上下文菜单中将看到“添加”。 Hover over "Add", you will see "Existing Item" and click it.
将鼠标悬停在“添加”上,您将看到“现有项目”并单击它。
Shift + Alt + A
is the shortcut for the above steps. Shift + Alt + A
是上述步骤的快捷键。
Navigate to where your .ps
file is and open it. 导航到
.ps
文件所在的位置,然后将其打开。 The .ps
file will now be in your C# Project. .ps
文件现在将在您的C#项目中。
Then, you need to look up how to execute Powershell files and reference the relative path. 然后,您需要查看如何执行Powershell文件并引用相对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.