C# 如何获取运行应用程序exe文件的目录?

C# 如何获取运行应用程序exe文件的目录?,c#,winforms,C#,Winforms,例如,假设我在c:\test安装了我的应用程序 然后从c:\test运行我的应用程序test.exe 因此,我想在程序中输入一个字符串,即目录c:\test 如果我从d:\hello运行test.exe 因此,我的程序中的目录将是d:\hello 安装是由InnoSetup完成的,但这只是为了设置我要安装到的目录以及从我的应用程序运行的目录 在我的申请中,我做到了: testDir = Path.GetDirectoryName(Application.ExecutablePath); 我以前

例如,假设我在c:\test安装了我的应用程序 然后从c:\test运行我的应用程序test.exe 因此,我想在程序中输入一个字符串,即目录c:\test 如果我从d:\hello运行test.exe 因此,我的程序中的目录将是d:\hello

安装是由InnoSetup完成的,但这只是为了设置我要安装到的目录以及从我的应用程序运行的目录

在我的申请中,我做到了:

testDir = Path.GetDirectoryName(Application.ExecutablePath);
我以前也试过:

testDir = Path.GetDirectoryName(Application.StartupPath);
在这两种情况下,我得到相同的异常无法找到文件

在安装后运行应用程序的地方有两个exe文件,一个是应用程序,另一个是应用程序需要使用的exe文件

所以我想从运行我的应用程序exe文件的目录中获取,这样我也可以使用另一个exe文件

编辑

守则:

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());
            }
        }
例外情况是在线路上抛出:

process = Process.Start(psi);
例外情况是:

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
ffmpeg.exe文件和应用程序exe文件都在程序文件中…等等 我可以从那里运行应用程序,但是应用程序在那里找不到ffmpeg.exe文件

编辑*

现在已尝试此代码:

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

与上面相同的异常。

方法返回目录路径,不带尾随斜杠。要使用它构建文件路径,您必须自己添加它

Assembly.GetExecutingAssembly().Location //is what you need here
最好是让框架为您做这件事,有时它不是普通的反斜杠:

ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);

堆栈跟踪是什么?你能发布试图查找文件的代码吗?如何从
testDir
构造其文件名?请注意
testDir=Path.GetDirectoryName(Application.StartupPath)是错误的。您应该只使用
testDir=Application.StartupPath否则您将是一个目录级别过高。Mathew没有相同的异常。您是否检查了
psi.FileName的值?(添加断点或将其打印出来)不,他确实需要
应用程序.ExecutablePath
(虽然
汇编.getexecutiongassembly().Location
会给出相同的答案)是的,但他使用的是
Path.GetDirectoryName()
。@MatthewWatson是的。那有什么区别呢?你为什么说这是错的?这没有错,只是和他已经尝试过的没有什么不同
testDir=Path.GetDirectoryName(Application.ExecutablePath)
@MatthewWatson如果他尝试了,但没有效果,为什么这不能成为一个替代解决方案?是的,在处理文件系统路径时,你应该始终使用
Path.Combine
。虽然我同意最好使用
Path.Combine()
,但我认为OP是在附加“@”\ffmpeg.exe”所以应该添加反斜杠。阴影向导尝试了您的代码示例“现在查看我的更新问题”与以前相同的异常。@joneK当您调试并检查值时,是否确认路径正确?