【问题标题】:how can i get the directory of where i run my application exe file from?如何获取运行应用程序 exe 文件的目录?
【发布时间】:2013-06-09 15:04:45
【问题描述】:

例如,假设我在 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());
            }
        }

与上述相同的异常。

【问题讨论】:

  • 堆栈跟踪是什么?
  • 你能贴出试图找到文件的代码吗?你是如何从testDir 构造它的文件名的?
  • 请注意testDir = Path.GetDirectoryName(Application.StartupPath); 是错误的。你应该只使用testDir = Application.StartupPath; 否则你的目录级别太高了。
  • Mathew 没有同样的异常。
  • 你检查过psi.FileName的值吗? (添加断点或打印出来)

标签: c# winforms


【解决方案1】:
Assembly.GetExecutingAssembly().Location //is what you need here

【讨论】:

  • 不,他真的需要Application.ExecutablePath(虽然Assembly.GetExecutingAssembly().Location会给出同样的答案)
  • 是的,但他正在使用Path.GetDirectoryName()
  • @MatthewWatson 对。那有什么区别呢?你为什么说这是错误的?
  • 没有错,只是和他已经尝试过的没什么不同。 testDir = Path.GetDirectoryName(Application.ExecutablePath);
  • @MatthewWatson 如果他尝试过但没有成功,为什么这不能成为替代解决方案?
【解决方案2】:

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

最好是让框架帮你做,有时候不是普通的反斜杠:

ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);

【讨论】:

  • 是的,在处理文件系统路径时,您应该始终使用Path.Combine
  • 虽然我同意最好使用 Path.Combine(),但我认为 OP 附加了 `"@"\ffmpeg.exe";"所以应该添加反斜杠。
  • Shadow Wizard 尝试了你的代码示例看看我更新的问题现在和以前一样的异常。
  • @joneK 当您调试并检查您确定路径正确的值?
猜你喜欢
  • 2010-09-13
  • 2015-01-23
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
相关资源
最近更新 更多