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

Ami*_*Pal 0 c# windows-installer cmd xcopy

我必须复制一个文件(在安装时)驻留在同一个文件夹中exe,msi 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();
Run Code Online (Sandbox Code Playgroud)

我对该installer类没有任何问题,因为它data.txt在给定路径上创建(在安装时).我应该怎么做,从复制文件directorySourcePath

我应该用cmd而不是xcopy吗?

更新

正如我所说,我希望到一个文件从那里同一个文件夹复制exeinstaller存在.当我安装我的应用程序.它显示一个错误:

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

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

我在代码中做了一些更改

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"));
Run Code Online (Sandbox Code Playgroud)

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

Eve*_*lie 5

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

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

File.Copy(sourceFileName, destFileName);
Run Code Online (Sandbox Code Playgroud)

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

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

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Run Code Online (Sandbox Code Playgroud)

  • @AmitPal,您肯定必须使用`cmd`可执行文件.你不能shell执行`xcopy`.但不要这样做 - 使用Evelie提供的方法. (3认同)