2013-11-20 143 views
0

我正在编写需要运行脚本的C#程序。我想将脚本包含在应用程序中,以便在用户在发布后安装程序时可用。在Visual Studio 2010 C#应用程序中添加脚本文件

我尝试添加脚本作为资源。在解决方案资源管理器的资源目录下,我可以看到脚本文件。

在节目中,我呼吁启动一个进程并运行所需的命令功能:

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

我得到消息“无法读取文件” \资源\命令提示符make.tcl “: 无此文件或目录”。所以我猜它找不到该文件?我没有正确引用文件吗?这是做这种事的正确方法吗?

回答

1

谢谢大家的建议。使用它们并进行更多的研究,我能够为我提出一个完美的解决方案。

1)将TCL脚本文件作为资源添加到项目中,并将Build Action设置为其“属性”中的“内容”。

2)获取路径TCL脚本(甚至从已发布的版本安装后):

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

3)使用所有必需的变量构建运行命令,并把它传递给可以执行常规它。

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); 

其中:

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(); 
    } 

这给了一些额外的津贴。由于该文件包含在应用程序中,但仍然是一个单独的实体,因此可以对其进行调整和修改,而无需重新构建,重新发布和重新安装该应用程序。

1

脚本运行器无法挖掘到您的可执行文件来查找命令,因为它很可能只知道如何处理磁盘上的文件。作为资源发货是一个好主意,但为了使它有用,您应该将其提取到磁盘上的真实文件中,以便其他程序可以使用它。

对于这样的事情,一个好的模式是在%TEMP%上创建一个临时文件,让脚本运行器执行该文件,然后将其删除。

+0

我明白了。你能否扩展一下如何去做这件事? – radensb

0

您需要确保将脚本文件的Build Action设置为Content以将其保存为独立文件。默认情况下,它将被设置为Resource,这意味着您必须以编程方式提取它并在尝试运行之前将其保存到临时位置。

+0

好的,我已经将构建操作更改为contnet,但我仍然收到相同的消息。我是否正确访问它? – radensb

+0

设置为“内容”的问题是您必须处理2个文件,而不是仅处理一个文件。使用'Resource',脚本被捆绑在.exe中,因此部署变得更容易。 – Alejandro

1

要扩展到Alejandro's answer,最简单的方法是使用临时文件夹,然后先将脚本复制到那里。

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. 
相关问题