简体   繁体   English

我怎么能得到我运行我的应用程序exe文件的目录?

[英]how can i get the directory of where i run my application exe file from?

For example let's say i installed my application at c:\\test Then from c:\\test i run my application test.exe So i want to get in a string in my program the directory c:\\test And if i run test.exe from d:\\hello So the directory in my program will be d:\\hello 例如,假设我在c:\\ test安装了我的应用程序然后从c:\\ test我运行我的应用程序test.exe所以我想在我的程序中输入一个字符串目录c:\\ test如果我运行test.exe来自d:\\ hello所以程序中的目录将是d:\\ hello

The installation is made by InnoSetup but thats just to set the directory i want to install to and to run from my application . 安装是由InnoSetup完成的,但这只是为了设置我想安装的目录并从我的应用程序运行。

In my application i did : 在我的应用程序中我做了:

testDir = Path.GetDirectoryName(Application.ExecutablePath);

And i tried before also : 我之前也尝试过:

testDir = Path.GetDirectoryName(Application.StartupPath);

In both cases im getting the same exception couldnt find the file... 在这两种情况下,我得到相同的异常无法找到该文件...

In the place i run my application from after installation there are two exe files one is the application the second is exe file the application need to work with. 在我从安装后运行我的应用程序的地方有两个exe文件,一个是应用程序,第二个是应用程序需要使用的exe文件。

So i want to get the directory from where i run my application exe file so i can also use the other exe file. 所以我想从我运行我的应用程序exe文件的目录,所以我也可以使用其他exe文件。

EDIT 编辑

The code : 编码 :

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

        public Ffmpeg()
        {
            workingDirectory = Path.GetDirectoryName(Application.ExecutablePath);// +@"\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;
                psi.Arguments = @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + outPath;
                Logger.Write("ProcessStartInfo Arguments" + @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + outPath);
                //psi.RedirectStandardOutput = true;
                process = Process.Start(psi);
                process.EnableRaisingEvents = false;
                p.WaitForConnection();
            }
            catch (Exception err)
            {
                Logger.Write("Exception Error: " + err.ToString());
            }
        }

The exception is throw up on the line : 例外情况就是上线:

process = Process.Start(psi);

The exception is : 例外是:

6/9/2013--6:11 PM ==> Exception Error: System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at ScreenVideoRecorder.Ffmpeg.Start(String pathFileName, Int32 BitmapRate) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Ffmpeg.cs:line 56

The ffmpeg.exe file and the application exe file both are in program files....etc I can run the application from there but the application cant find the ffmpeg.exe file in there ffmpeg.exe文件和应用程序exe文件都在程序文件....等我可以从那里运行应用程序但应用程序无法在那里找到ffmpeg.exe文件

EDIT * 编辑*

Tried this code now : 现在尝试这个代码:

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

        public Ffmpeg()
        {
            workingDirectory = Application.StartupPath; //Path.GetDirectoryName(Application.ExecutablePath);// +@"\workingDirectory";
            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }
            ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);//@"\ffmpeg.exe";
            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;
                psi.Arguments = @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + outPath;
                Logger.Write("ProcessStartInfo Arguments" + @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + outPath);
                //psi.RedirectStandardOutput = true;
                process = Process.Start(psi);
                process.EnableRaisingEvents = false;
                p.WaitForConnection();
            }
            catch (Exception err)
            {
                Logger.Write("Exception Error: " + err.ToString());
            }
        }

Same exception as above . 与上述相同的例外。

Assembly.GetExecutingAssembly().Location //is what you need here

The Path.GetDirectoryName() method returns the directory path without trailing slash . Path.GetDirectoryName()方法返回没有尾部斜杠的目录路径。 To build a file path using it, you have to add that yourself. 要使用它构建文件路径,您必须自己添加它。

Best is to let the framework do it for you, sometimes it's not the ordinary backslash: 最好是让框架为你做,有时它不是普通的反斜杠:

ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM