例如,假设我将我的应用程序安装在 c:\test 然后从 c:\test 我运行我的应用程序 test.exe 所以我想在我的程序中获取一个字符串目录 c:\test 如果我运行 test.exe from d:\hello 所以我程序中的目录将是 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());
}
}
与上述相同的例外。