0

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

The installation is made by InnoSetup but thats just to set the directory i want to install to and to run from my application .

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.

So i want to get the directory from where i run my application exe file so i can also use the other exe file.

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

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 .

10
  • What's the stack trace?
    – SLaks
    Commented Jun 9, 2013 at 15:06
  • Can you post the code that is trying to find the file? How are you constructing its filename from testDir? Commented Jun 9, 2013 at 15:06
  • Note that testDir = Path.GetDirectoryName(Application.StartupPath); is wrong. You should just use testDir = Application.StartupPath; otherwise you'll be one directory level too high. Commented Jun 9, 2013 at 15:11
  • Mathew nope same exception .
    – joneK
    Commented Jun 9, 2013 at 15:16
  • Have you checked the value of psi.FileName? (Add breakpoints or print it out)
    – Alvin Wong
    Commented Jun 9, 2013 at 15:26

2 Answers 2

2
Assembly.GetExecutingAssembly().Location //is what you need here
6
  • 1
    No, he really needs Application.ExecutablePath (although Assembly.GetExecutingAssembly().Location will give the same answer) Commented Jun 9, 2013 at 15:07
  • Yes but he's using Path.GetDirectoryName() with it. Commented Jun 9, 2013 at 15:08
  • @MatthewWatson Right. What's the difference then? Why are you saying this is wrong? Commented Jun 9, 2013 at 15:09
  • It's not wrong, it's just no different from what he already tried. testDir = Path.GetDirectoryName(Application.ExecutablePath); Commented Jun 9, 2013 at 15:10
  • @MatthewWatson If he tried that and it didn't work, why couldn't this be an alternative solution? Commented Jun 9, 2013 at 15:11
0

The Path.GetDirectoryName() method returns the directory path without trailing slash. 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);
4
  • Yeah you should always use Path.Combine when working with filesystem paths.
    – Alvin Wong
    Commented Jun 9, 2013 at 15:22
  • While I agree that it's best to use Path.Combine() I think the OP was appending `"@"\ffmpeg.exe";" so the backslash should have been added. Commented Jun 9, 2013 at 15:22
  • Shadow Wizard tried your code example look my updated question now same exception as before .
    – joneK
    Commented Jun 9, 2013 at 15:40
  • @joneK when you debug and check the value you sure the path is correct? Commented Jun 9, 2013 at 17:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.