简体   繁体   English

在Visual Studio 2010 C#应用程序中添加脚本文件

[英]Adding a script file in a Visual Studio 2010 C# application

I am writing a C# program that needs to run a script. 我正在编写一个需要运行脚本的C#程序。 I want to include the script with the application so that it is available when the user installs the program after I publish it. 我想在应用程序中包含脚本,以便在我发布程序后用户安装程序时可以使用该脚本。

I tried adding the script as a resource. 我尝试将脚本添加为资源。 In the Solution Explorer, under the Resources directory, I can see the script file. 在解决方案资源管理器的“资源”目录下,可以看到脚本文件。

In the program, I call a function that starts a process and runs the desired command: 在程序中,我调用一个函数来启动进程并运行所需的命令:

runNewProcess("tclsh \\Resources\\make.tcl " + activeProducts);

I get the command prompt with the message "couldn't read file "\\Resources\\make.tcl": no such file or directory". 我收到命令提示符,并显示消息“无法读取文件“ \\ Resources \\ make.tcl”:没有此类文件或目录”。 So I guess it cannot find the file? 所以我猜它找不到文件? Am I not referencing the file correctly? 我没有正确引用文件吗? Is this the correct way of doing something like this? 这是做这样的事情的正确方法吗?

The script runner is unable to dig into you executable to find the commands, as it most likely only know what to do with files on disk. 脚本运行程序无法深入您的可执行文件中查找命令,因为它很可能只知道如何处理磁盘上的文件。 Shipping as a resource is a good idea, but for make anything useful with it you should extract it into a real file on disk so that other programs can use it. 作为资源进行运输是一个好主意,但是为了使它有用,您应该将其提取到磁盘上的真实文件中,以便其他程序可以使用它。

A good pattern for such things would be to create a temporary file on %TEMP%, make the script runner execute that file, and delete it afterwards. 进行此类操作的一个好方法是在%TEMP%上创建一个临时文件,使脚本运行程序执行该文件,然后将其删除。

To expand on Alejandro's answer , The easiest way to handle this is to use the temporary folder and copy your script there first. 要扩展Alejandro的答案 ,最简单的方法是使用临时文件夹,然后首先在其中复制脚本。

var scriptPath = Path.Combine(Path.GetTempPath(), "make.tcl");

// Copy the text of the script to the temp folder. There should be a property 
//you can reference associated with the script file if you added the file using 
//the resources tab in the project settings. This will have the entire script in
//string form.
File.WrteAllText(scriptPath, Resources.make);

runNewProcess("tclsh \"" + scriptPath + "\"" + activeProducts); //added quotes in case there are spaces in the path to temp.

File.Delete(scriptPath); //Clean up after yourself when you are done.

Thank you all for your suggestions. 谢谢大家的建议。 Using them and with a bit more research, I was able to come up with a perfect solution for me. 使用它们并进行更多研究,我为我提供了一个完美的解决方案。

1) Add the TCL script file as a resource to the project and set the Build Action to 'Content' in it's Properties. 1)将TCL脚本文件作为资源添加到项目中,然后在其“属性”中将“构建操作”设置为“内容”。

2) Get the path to the TCL script (even after installation from a published version): 2)获取TCL脚本的路径(即使从发布版本安装后):

string makeScriptPath = System.Windows.Forms.Application.StartupPath + "\\Resources\\make.tcl";

3) Construct the run command using all the required variables and pass it to a routine that can execute it. 3)使用所有必需的变量构造run命令,并将其传递给可以执行该命令的例程。

localCommand = String.Format("tclsh \"{0}\" --librarytype {1} --makeclean {2} --buildcode {3} --copybinary {4} --targetpath \"{5}\" --buildjobs {6} --products {7}",
                                       makeScriptPath, library, makeClean, buildCode, copyBinary, targetPath, buildJobs, activeProducts);
                runNewProcess(localCommand);

where: 哪里:

    private void runNewProcess(string command)
    {
        System.Diagnostics.ProcessStartInfo procStartInfo =
            new System.Diagnostics.ProcessStartInfo("cmd", "/k " + command);
        procStartInfo.RedirectStandardOutput = false;
        procStartInfo.UseShellExecute = true;
        procStartInfo.CreateNoWindow = true;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process proc = new System.Diagnostics.Process();

        proc.StartInfo = procStartInfo;
        proc.Start();
    }

This gives some added perks. 这给了一些额外的好处。 Since the file is included with the application, but remains a separate entity, this allows it to be tweaked and modified without needing to re-build, re-publish and re-install the application. 由于该文件包含在应用程序中,但仍然是一个单独的实体,因此可以对其进行调整和修改,而无需重新生成,重新发布和重新安装应用程序。

You need to ensure that the Build Action of the script file is set to Content to keep it as an independent file. 您需要确保将脚本文件的“ Build Action设置为“ Content以将其保留为独立文件。 By default it will be set to Resource which means you will have to programmatically extract it and then save it to a temporary location before attempting to run it. 默认情况下,它将设置为Resource ,这意味着您必须以编程方式提取它,然后将其保存到临时位置,然后再尝试运行它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2010 C#-将文件浏览器中的保存文件加载到应用程序中 - Visual Studio 2010 C# - Loading saved file from file explorer into application 在 Visual Studio C# 2010 的父目录中添加项目 - Adding an item that is in a parent directory in Visual Studio C# 2010 使用c#将Access数据库添加到Visual Studio 2010中的项目中 - Adding Access Database to Project in Visual Studio 2010 using c# 即使成功构建项目,应用程序也无法与MDF文件建立连接[Visual Studio 2010 C#] - The application cannot make a connection with MDF file even building the project successfully [Visual Studio 2010 C#] 断言在C#WPF应用程序的Visual Studio 2010中不起作用 - Asserts not working in Visual Studio 2010 in a C# WPF application 如何在Visual Studio 2010中向C#Web应用程序添加计时器 - How to add a timer to c# Web Application in Visual studio 2010 C#Visual Studio 2010表单应用程序错误消息 - C# Visual Studio 2010 form application error message 在Visual Studio 2010 C#项目中读取链接的xml文件 - Read a linked xml file in Visual Studio 2010 C# project 在Visual Studio 2010 C#项目中将.chm文件链接到.dll - Linking a .chm file to a .dll in Visual Studio 2010 C# Project 如何将dll项目设置整合到Visual Studio 2010中用于C#应用程序的主exe.config文件中? - How can I consolidate my dll project settings into my main exe.config file in Visual Studio 2010 for a C# application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM