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

C# 如何在安装exe时将文件复制到指定路径?,c#,cmd,windows-installer,xcopy,C#,Cmd,Windows Installer,Xcopy,我必须将位于exe和msi installer所在文件夹中的文件(安装时)复制到其他路径。为此,我在installer类中编写了以下代码 System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.Wind

我必须将位于
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();
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
我对
installer
类没有任何问题,因为它正在给定路径上创建
data.txt
(安装时)。如何将文件从
目录
复制到
源路径

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
我应该使用
cmd
而不是
xcopy

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
更新 正如我提到的,我想从存在
exe
installer
的同一文件夹中复制一个文件。当我安装我的应用程序时。它显示了一个错误:

Unable to find the file from "C:\Program Files (x86)\Default Company Name\inataller". 
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
它正在尝试从
程序文件
目录中选取文件。但是它应该是我的
exe
所在的目录。我不想
硬编码
exe的路径,因为它将分发到其他客户端。从同一文件夹中拾取文件的适当代码是什么

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
我对代码做了一些更改

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"));
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

现在显示:
对象引用未设置为对象的实例

如果要将myFile.exe从“目录”位置复制到“源路径”

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
sourcefilename只是要复制文件的位置,destFileName是要复制文件的目标位置。包括文件名

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
至于获取exe的位置,您可以使用

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

如果要将myFile.exe从“目录”位置复制到“源路径”

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
sourcefilename只是要复制文件的位置,destFileName是要复制文件的目标位置。包括文件名

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
至于获取exe的位置,您可以使用

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

忽略到目前为止所做的工作,创建一个新的安装程序,并像往常一样将证书文件编写到其中。构建MSI并运行以下命令:

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
msiexec /a foo.msi TARGETDIR=C:\EXTRACT /qb
现在看看C:\EXTRACT。您将看到一个未压缩的MSI和一个文件目录结构。使用要部署的文件覆盖CER文件。现在在计算机上运行该MSI,并注意部署了哪个CER文件

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

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

忽略您迄今为止所做的工作,创建一个新的安装程序,并像往常一样将证书文件写入其中。构建MSI并运行以下命令:

string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
msiexec /a foo.msi TARGETDIR=C:\EXTRACT /qb
现在看看C:\EXTRACT。您将看到一个未压缩的MSI和一个文件目录结构。使用要部署的文件覆盖CER文件。现在在计算机上运行该MSI,并注意部署了哪个CER文件

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


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

谢谢你的回复,但我想用
cmd
使用
processStartInfo
来处理这个问题,有什么具体的原因吗?没有这样的原因。那么
sourceFileName
destfileName
呢?它们是否符合我的要求?@AmitPal,您肯定必须使用
cmd
可执行文件。您不能shell执行
xcopy
。但不要这样做-使用Evlie提供的方法。@AmitPal编辑了一篇文章,解释了参数。谢谢回复,但我想用
cmd
使用
processStartInfo
来处理这个问题。有什么具体的原因吗?没有这样的原因。那么
sourceFileName
destfileName
呢?它们是否符合我的要求?@AmitPal,您肯定必须使用
cmd
可执行文件。您不能shell执行
xcopy
。但不要这样做-使用Evlie提供的方法。@AmitPal编辑了一篇文章,解释了参数。你的设计与Windows Installer完全不兼容。你到底想做什么?您是否需要将证书发布到证书存储区或其他地方?这是否只是一个需要逐个客户对安装进行改造的问题?有更优雅、更健壮的方法来实现这一点。@ChristopherPaint基本上我需要在安装我的exe时将我的证书文件复制到
c:\program files(x86)\Microsoft SDK\windows\v7.0A\Bin
。文件名是否更改?根据您使用的工具,一种简单的方法是从MSI创建管理安装,然后覆盖CER文件。然后,当用户从该点安装时,它将拾取修改后的文件。没有文件名没有更改。实际上,安装程序无法从
目录
中选择文件(请参见更新的部分代码)。@ChristopherPaint在
目录
中设置硬编码路径后,我尝试了以下操作:(您的设计与Windows Installer完全不兼容。您到底想做什么?是否需要将证书发布到证书存储区或其他地方?这是否只是一个需要逐个客户转换安装的问题?实现这一点的方法要优雅得多。@Chri)stopherPainter基本上我需要在安装我的exe时将我的证书文件复制到
c:\program files(x86)\Microsoft SDK\windows\v7.0A\Bin
。文件名是否更改?根据您使用的工具,一种简单的方法是创建管理文件
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);