如何在安装exe时将文件复制到指定的路径?

时间:2013-02-25 13:34:35

标签: c# cmd windows-installer xcopy

我必须将存在exemsi installer的同一文件夹中的文件(安装时)复制到某个不同的路径。为此,我在installer类中编写了以下代码。

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new     
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "xcopy";
startInfo.UseShellExecute = true;
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string SourcePath = Path.GetFullPath("C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin");
StreamWriter sw = new StreamWriter(@"C:\Users\lovestone\Desktop\data.txt");
sw.WriteLine(directory);
sw.WriteLine(SourcePath);
startInfo.Arguments = "\"" + directory + "\"" + " " + "\"" + SourcePath + "\"" + @" /e /y /I";
process.StartInfo = startInfo;
process.Start();

我对installer类没有任何问题,因为它在给定路径上创建data.txt(在安装时)。我该如何将文件从directory复制到SourcePath

我应该使用cmd代替xcopy吗?

已更新

正如我提到的那样,我想从存在exeinstaller的同一文件夹中复制文件。当我安装我的应用程序。它显示错误:

Unable to find the file from "C:\Program Files (x86)\Default Company Name\inataller". 

它正试图从program files目录中选择文件。但它应该与我的exe存在的目录相同。我不想hard-coded exe的路径,因为它将分发给其他客户端。从同一个文件夹中选择文件的相应代码是什么?

我在代码中做了一些更改

string directory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string SourcePath = Path.GetFullPath("C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin");
File.Copy(Path.Combine(directory, "MyAdHocTestCert.cer"),Path.Combine(SourcePath, "MyAdHocTestCert.cer"));

现在显示:Object reference not set to an instance of an object

2 个答案:

答案 0 :(得分:5)

如果要将myFile.exe从“目录”位置复制到“SourcePath”。

string sourceFileName = Path.Combine(directory, "myFile.exe");
string destFileName = Path.Combine(SourcePath, "myFileCopy.exe");

File.Copy(sourceFileName, destFileName);

sourcefilename只是您要复制的文件的位置,destFileName是您要复制它的目标。包括文件名。

至于获取exe的位置,你可以使用

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

答案 1 :(得分:0)

忽略您到目前为止所做的事情,创建一个新的安装程序并像平常一样将证书文件写入其中。构建MSI并运行命令:

msiexec /a foo.msi TARGETDIR=C:\EXTRACT /qb

现在看一下C:\ EXTRACT。您将看到未压缩的MSI和文件的目录结构。使用要部署的文件覆盖CER文件。现在在机器上运行该MSI并记下部署了哪个CER文件。

真的应该那么简单。如果您使用的是更好的工具,例如InstallShield或WiX,您可以构建部分压缩的MSI和未压缩的单个文件。不需要所有这些可怕的自定义动作反模式,其中MSI调用DLL调用的CMD调用的XCOPY。顺便说一句,你知道VDPROJ已经从VS2012中删除了,对吗?