【问题标题】:How can i set the directory to where my application was installed?如何将目录设置为我的应用程序的安装位置?
【发布时间】:2013-06-08 21:19:59
【问题描述】:

我使用 innosetup 来安装我的应用程序。 例如,所有文件都在程序文件\测试中 在目录中,我有我的程序的 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 and Copy always

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

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

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

【问题讨论】:

  • 所以在安装过程中为用户选择的安装路径设置一个注册表项。在您的应用程序中读取该路径
  • 我认为您没有正确创建安装包。一个工作的安装包会非常完整,以至于用户什么都不用做。安装后,开始菜单中有一些快捷方式,您的用户可以在其中找到如何运行您的应用程序。

标签: c# winforms


【解决方案1】:

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

string fullPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string installDirectory = Path.GetDirectoryName( fullPath );

但是,如果您真的想将该文件从安装目录复制到 Application.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 文件放置在 workingDirectory 中?这将解决您在这里的所有问题。

【讨论】:

  • 我想他想把他的ffmpeg.exe放在文件夹Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\workingDirectory"
  • 如果和主.exe在同一个文件夹里,那就太简单了。
  • 好吧,我认为 OP 应该澄清这一点,因为它非常令人困惑。
  • 我同意,这太令人困惑了。他应该用另一种方式更清楚地表达问题,不需要他发布的太多代码。
  • 但是,为什么他在安装过程中没有将它复制到正确的位置?我不知道 InnoSetup,但我认为该程序具有在任意路径中复制文件的所有功能
【解决方案2】:

1- 您应该创建一个注册表项,该注册表项可以存储您的应用程序需要的任何其他文件夹的安装路径和路径。检查这个问题如何做到这一点:How to write install path to registry after install is complete with Inno setup

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

【讨论】:

  • 他的问题不在于路径是什么,他事先已经知道路径Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\workingDirectory",因为该路径是Windows操作系统中确实存在的常见路径(子文件夹workingDirectory是由他的应用程序创建的)。他的问题是该文件夹中没有ffmpeg.exe。正如我所说,它涉及安装项目。这就是安装项目的工作。
  • King King 好吧,你是对的,所以我需要告诉安装程序 InnoSetup 在某处创建我想要的目录,例如 workingDirectory,然后在安装时将 ffmpeg.exe 复制到该目录,对吗?跨度>
  • @joneK 是的,这就是你必须尝试理解的内容,找出如何做到这一点。如果使用 Visual Studio 安装项目,我可以提供帮助。但是对于你的innoSetup,我不能。
  • Kink King Visual Studio 的设置非常基本,不是吗?我有visual studio 2012 pro,你能逐步告诉我如何处理文件以及如何添加文件图标的东西吗?出于某种原因,当我在我的应用程序属性中选择发布时,我有一个选项非常有限的向导。
  • ok.也许试试这个问题:stackoverflow.com/questions/13537071/…
【解决方案3】:

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

Application.StartupPath

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

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

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

 File.Copy(Source, Target)

http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx

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

【讨论】:

  • 我认为这不是 OP 想要的。他的ffmpeg.exe文件应该位于文件夹Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\workingDirectory"
  • 我认为他想将他的ffmpeg.exe 放在那个文件夹中,但是他的安装包没有将ffmpeg.exe 移动到那个文件夹中,这就是为什么它是空的。所以问题是如何在安装时将其移动到该文件夹​​。我认为你低估了这里的问题。
  • 但它不是必需的...... ffmpeg 应该在那个应用程序路径中......在 AplicationDataPath 他应该只存储数据而不是应用程序
  • 是的,如果我是他,我也想将它放在与主应用程序路径相同的文件夹(或相对文件夹)中。但这正是他想要的。 :)
  • 顺便说一句,他只需要使用 File.Copy(source, target) 将 ffmpeg.exe 从 Application.StartupPath 复制到 Application.LocalUserAppData
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
相关资源
最近更新 更多