How can i get the path of the directory i run my application exe file from ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
For example :
Im using InnoSetup and i installed my application to C:Test
In the directory Test i have now two files : ffmpeg.exe and myapp.exe
I can run myapp.exe but myapp.exe also need ffmpeg.exe to work .
So what i need is to get the path/directory of from where i run my application .
This is my code in my class i did :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";
Logger.Write("workingDirectory: " + workingDirectory);
if (!Directory.Exists(workingDirectory))
{
Directory.CreateDirectory(workingDirectory);
}
ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);//@"ffmpeg.exe";
Logger.Write("FfmpegFilename: " + 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 \.pipemytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + outPath;
Logger.Write("ProcessStartInfo Arguments" + @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \.pipemytestpipe -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());
}
}


workingDirectory should contain the directory of the exe file of my application after installation .
But all the time i tried many ways im getting an exception say file not found the ffmpeg.exe
Here is my log file content :
6/9/2013--10:05 PM ==> workingDirectory: D:C-SharpScreenVideoRecorderScreenVideoRecorderWorkingVersionbinDebug
6/9/2013--10:05 PM ==> FfmpegFilename: D:C-SharpScreenVideoRecorderScreenVideoRecorderWorkingVersionbinDebugffmpeg.exe
6/9/2013--10:05 PM ==> Output Video File Directory: C:Usersbout0_000AppDataLocalScreenVideoRecorderScreenVideoRecorderDefaultDirectory
6/9/2013--10:05 PM ==> Frame Rate: 25
6/9/2013--10:05 PM ==> ProcessStartInfo Arguments-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \.pipemytestpipe -map 0 -c:v libx264 -r 25 C:Usersbout0_000AppDataLocalScreenVideoRecorderScreenVideoRecorderDefaultDirectory
6/9/2013--10:06 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-SharpScreenVideoRecorderScreenVideoRecorderWorkingVersionFfmpeg.cs:line 56
line 56 is:process = Process.Start(psi);

I cant find out why im getting the exception .
Now when im running the application from the visual studio then its looking for the ffmpeg.exe in the Debug directory so i copied now the ffmpeg.exe to the Debug and its working now good.
But after installation if the application exe file and the ffmpeg.exe file are both in some directory like c: program filestest
Then im getting the exception it cant find the file.
The log file show the missing file exception then i copied ffmpeg.exe to the debug and its ok.
But if its not debug if i run my application from c:program filestest
Then im getting the exception even if the ffmpeg.exe is there.
Now when im running the program from another directory not from the visual studio im getting exception :6/9/2013--10:15 PM ==> Error: System.IO.IOException: Pipe is broken.
at System.IO.Pipes.PipeStream.CheckWriteOperations()
at System.IO.Pipes.PipeStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at ScreenVideoRecorder.Ffmpeg.PushFrame(Bitmap bmp)

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";
Logger.Write("workingDirectory: " + workingDirectory);
if (!Directory.Exists(workingDirectory))
{
Directory.CreateDirectory(workingDirectory);
}
ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);//@"ffmpeg.exe";
Logger.Write("FfmpegFilename: " + 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 \.pipemytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + outPath;
Logger.Write("ProcessStartInfo Arguments" + @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \.pipemytestpipe -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());
}
}

public void PushFrame(Bitmap bmp)
{
try
{
int length;
// Lock the bitmaps bits.
//bmp = new Bitmap(1920, 1080);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
//Rectangle rect = new Rectangle(0, 0, 1280, 720);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bmp.PixelFormat);

int absStride = Math.Abs(bmpData.Stride);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
//length = 3 * bmp.Width * bmp.Height;
length = absStride * bmpData.Height;
byte[] rgbValues = new byte[length];

//Marshal.Copy(ptr, rgbValues, 0, length);
int j = bmp.Height - 1;
for (int i = 0; i < bmp.Height; i++)
{
IntPtr pointer = new IntPtr(bmpData.Scan0.ToInt32() + (bmpData.Stride * j));
System.Runtime.InteropServices.Marshal.Copy(pointer, rgbValues, absStride * (bmp.Height - i - 1), absStride);
j--;
}
p.Write(rgbValues, 0, length);
bmp.UnlockBits(bmpData);
}
catch(Exception err)
{
Logger.Write("Error: " + err.ToString());
}

On the line p.Write(rgbValues, 0, length);
But thats since for some reason it didnt catch the exception in process that the file is not found.
-----------------------------------------------------------------------------------------------------------
1. I need to get the directory of the ffmpeg.exe from where the program is installed not the Debug !
2. If i run it from visual studio its working if the ffmpeg.exe is in the Debug directory.
3. If i run the program from another directory not from the visual studio just run the exe its not finiding the ffmpeg.exe for some reason.
How can i fix it ?

View the full article
 
Back
Top