C# 如何将安装应用程序的目录设置为?

C# 如何将安装应用程序的目录设置为?,c#,winforms,C#,Winforms,我使用innosetup安装我的应用程序。 例如,所有文件都在program files\test中 在目录中,我有我的程序的exe文件,还有ffmpeg.exe 现在在我的代码中,我做到了: class Ffmpeg { NamedPipeServerStream p; String pipename = "mytestpipe"; byte[] b; System.Diagnostics.Process process;

我使用innosetup安装我的应用程序。 例如,所有文件都在program files\test中 在目录中,我有我的程序的exe文件,还有ffmpeg.exe

现在在我的代码中,我做到了:

class Ffmpeg
    {
        NamedPipeServerStream p;
        String pipename = "mytestpipe";
        byte[] b;
        System.Diagnostics.Process process;
        string ffmpegFileName;
        string workingDirectory;

        public Ffmpeg()
        {
            workingDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\workingDirectory";
            ffmpegFileName = @"\ffmpeg.exe";
            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }
            ffmpegFileName = workingDirectory + ffmpegFileName;
            Logger.Write("Ffmpeg Working Directory: " + ffmpegFileName);
        }

        public void Start(string pathFileName, int BitmapRate)
        {
            try
            {
                string outPath = pathFileName;
                Logger.Write("Output Video File Directory: " + outPath);
                Logger.Write("Frame Rate: " + BitmapRate.ToString());
                p = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte);
                b = new byte[1920 * 1080 * 3]; // some buffer for the r g and b of pixels of an image of size 720p

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                psi.CreateNoWindow = true;
                psi.FileName = ffmpegFileName;
                psi.WorkingDirectory = workingDirectory;
问题是安装后目录workingDirectory不包含ffmpeg.exe。因此,如果用户在安装后第一次运行程序,则文件将丢失

我将ffmpeg.exe添加到我的项目中,并将其设置为:Content和Copy always

我想做的是,以某种方式将workingDirectory设置为用户安装程序的位置,如果它是程序文件或任何其他目录

或者将workigDirectory设置为我已经添加到项目中的文件ffmpeg.exe


问题是安装后,用户将运行程序,目录workingDirectory将为空。

如果文件
ffmpeg.exe
安装在调用它的程序集所在的同一目录中,则您可以写入:

string fullPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string installDirectory = Path.GetDirectoryName( fullPath );
但是,如果确实要将该文件从安装的目录复制到
应用程序。LocalUserAppDataPath

// Assuming that the LocalUserAppDataPath has already been created
string destDirectory = Path.Combine(Application.LocalUserAppDataPath, "workingDirectory");
File.Copy(Path.Combine(installDirectory, "ffmpeg.exe"), 
          Path.Combine(destDirectory, "ffmpeg.exe"), true);

但是,为什么不搜索InnoSetup的功能,以发现在安装过程中如何将
ffmpeg.exe
文件放入
工作目录
?这将解决您在此处遇到的所有问题。

您可以使用Application.StartupPath引用主可执行文件和ffmpeg.exe所在的文件夹路径

应用程序。StartupPath

获取启动应用程序的可执行文件的路径, 不包括可执行文件名

如果你需要的话。。。将ffmpeg.exe复制到您选择的工作目录。。虽然我认为这不是一个好主意

 File.Copy(Source, Target)


1-您应该创建一个注册表项,该注册表项可以存储安装路径和应用程序所需的任何其他文件夹的路径。检查此问题,了解如何执行此操作:


2-在应用程序启动时读取注册表设置。使用这些路径。

因此,在安装过程中为用户选择的安装路径设置一个注册表项。在应用程序中阅读该路径。我认为您没有正确创建安装包。一个工作的安装包将是如此完整,用户无需做任何事情。安装后,“开始”菜单中有一些快捷方式,您的用户可以在其中找到如何运行应用程序。我想他希望将他的
ffmpeg.exe
放在
Path.GetDirectoryName(application.LocalUserAppDataPath)+@“\workingDirectory”
文件夹中,如果它与main.exe位于同一文件夹中,这很简单。我认为OP应该澄清这一点,因为它非常令人困惑。我同意,它非常令人困惑。他应该用另一种更清晰的方式来表达问题,在他发布时不需要太多的代码。但是,为什么他在安装过程中没有将其复制到正确的位置呢?。我不知道InnoSetup,但我认为该程序具有以任意路径复制文件的所有功能。我不认为这是OP想要的。他的
ffmpeg.exe
文件应该位于
Path.GetDirectoryName(Application.LocalUserAppDataPath)+@“\workingDirectory”
文件夹中,我想他想把他的
ffmpeg.exe
放在那个文件夹中,但他的安装包没有移动到那个文件夹中,所以它是空的。因此,问题是如何在安装时将其移动到该文件夹。我想你低估了这里的问题,但这是不必要的。。。ffmpeg应该位于该应用程序路径中。。。在应用程序数据路径中,他应该只存储数据而不是应用程序,如果我是他,我还希望将其与主应用程序路径放在同一文件夹(或相对文件夹)中。但那是他想要的顺便说一句,他只需要使用File.copy(源、目标)将ffmpeg.exe从Application.StartupPath复制到Application.LocalUserAppData。他的问题不是路径是什么,他事先已经知道路径
path.GetDirectoryName(Application.LocalUserAppDataPath)+@“\workingDirectory”
因为该路径是Windows操作系统中存在的公共路径(子文件夹
workingDirectory
由他的应用程序创建)。他的问题是该文件夹中没有
ffmpeg.exe
。正如我所说,这涉及到安装项目。这是安装项目的工作。King King好的,你说得对,所以我需要告诉安装程序InnoSetup创建我想要的目录,例如某个地方的workingDirectory,然后在安装时将ffmpeg.exe复制到这个目录,对吗?@joneK是的,这是你必须尝试理解的,找出如何做到这一点。如果使用Visual Studio安装项目,我可以提供帮助。但是对于您的
innoSetup
,我不能。visual studio设置非常基本不?我有visual studio 2012 pro,你能一步一步地告诉我如何处理文件以及如何添加文件图标吗?由于某些原因,当我在“应用程序属性”中选择“发布”时,我有一个选项非常有限的向导。好的。请尝试以下问题:
string destDirectory = Path.Combine(Application.LocalUserAppDataPath, "workingDirectory");
string ffmpegDestPath = Path.Combine(destDirectory, "ffmpeg.exe");
string ffmpegSrcPath = Path.Combine(Application.StartupPath, "ffmpeg.exe");

if (!File.Exist(ffmpegDestPath)) {
     if (!Directory.Exists(destDirectory)) Directory.Create(destDirectory);
     File.Copy(ffmpegSrcPath , ffmpegDestPath );
}