簡體   English   中英

如何設置應用程序安裝目錄?

[英]How can i set the directory to where my application was installed?

我使用了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 and Copy always

我想要做的是,以某種方式將workingDirectory設置為用戶正在安裝程序的位置(如果它是程序文件或任何其他目錄)。

或將workigDirectory設置為我已添加到項目中的文件ffmpeg.exe。

問題是安裝后,用戶將運行該程序,並且目錄workingDirectory將為空。

如果將文件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中? 這樣可以解決您的所有問題。

您可以使用Application.StartupPath引用文件夾路徑,其中是您的主要可執行文件和ffmpeg.exe。

應用程序。啟動路徑

獲取啟動應用程序的可執行文件的路徑,不包括可執行文件的名稱。

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.application.startuppath.aspx

然后如果需要的話...將ffmpeg.exe復制到您選擇的工作目錄中。盡管我認為這不是一個好主意...

 File.Copy(Source, Target)

http://msdn.microsoft.com/zh-CN/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 );
}

1-您應該創建一個注冊表項,該注冊表項可以存儲安裝路徑以及應用程序需要的任何其他文件夾的路徑。 檢查有關如何執行此問題的問題: 使用Inno安裝程序完成安裝后如何將安裝路徑寫入注冊表

2-在應用程序啟動時閱讀注冊表設置。 使用這些路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM