How to use lame.exe in my application? - c#

I capture audio files in the wave format in my Microsoft Surface application. Now for file size reasons, I'd like to convert the wave file into a mp3 file. I read in the Internet that a good possibility to do that is using lame.
But how can I call this exe file from my application? and how can I include it into my application?

Use Process class to call an external application:
string lameEXE = #"C:\path_of_lame\lame.exe";
string lameArgs = "-V2";
string wavFile = #"C:\my_wavs\input.wav";
string mp3File = #"C:\my_mp3s\output.mp3";
Process process = new Process();
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = lameEXE;
process.StartInfo.Arguments = string.Format(
"{0} {1} {2}",
lameArgs,
wavFile,
mp3File);
process.Start();
process.WaitForExit();
int exitCode = process.ExitCode;

You can call an executable from .NET by using the System.Diagnostics.Process class and related classes - see here for the documentation.
Lame has pretty robust command line arguments, which can be found here. You can pass command line arguments to the Process using the ProcessStartInfo.Arguments property.

public void mciConvertWavMP3(string fileName, bool waitFlag)
{
//maxLen is in ms (1000 = 1 second)
string outfile= "-b 32 --resample 22.05 -m m \"" + pworkingDir+fileName + "\" \"" + pworkingDir + fileName.Replace(".wav",".mp3") + "\"";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "\"" + pworkingDir + "lame.exe" + "\"";
psi.Arguments = outfile;
//psi.WorkingDirectory = pworkingDir;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
if (waitFlag)
{
p.WaitForExit();
// wait for exit of called application
}
}
Above code taken from here .
Depending on the usage, you can incorporate a Process.StartInfo object, control properties such as ShellExecute and also redirect any output from the application to (say) a log file or UI component.
To bundle the exe with your project, check this question from stackoverflow out. Personally, I'd go with the first suggestions:
There are several ways you could
accomplish this. First, you should add
program.exe to the project. You would
do this by right-clicking the project
in Visual Studio, and selecting Add >
Existing Item... Select program.exe,
and it will appear in the project.
Viewing its properties, you can set
"Copy to Output Directory" to "Copy
Always", and it will appear in your
output directory beside your
application.
If you stick to the above method, then reference lame.exe relatively ('....\Tools\Lame.exe' for example).
Finally, according to the official lame site : RareWares offers several compiled LAME versions, including modified versions featuring special functionality.

There is a DLL version of LAME, I would be surprised if you can't find a VB or C# example using it. Check this discussion thread: http://www.eggheadcafe.com/software/aspnet/31294459/-lameencdll-and-vbnet.aspx

Related

C# Change folder and start process in CMD, wait for it to finish

I need to open an external process from my Windows Forms app. What's more, I need to do it several times during the app's run time. Basically, I'm executing a .exe file with arguments several times from the command prompt, however I need to change the folder to where the .exe is in order for it to work properly.
So far, I'm opening the cmd like so:
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
Process process = Process.Start(processStartInfo);
Then using the process.WriteLine to feed commands to the CMD.
process.StandardInput.WriteLine("Awin.exe -X " + filePath + "/" + fileNumber + " ID=\"" + id + "\"");
And I need t do this for several fileNumber files. Also, I need to wait for the process started from the input to end before going on to the next one.
Is there a better way to do this, because I'm not getting good results from using process.WaitForExit
ProcessStartInfo class has a WorkingDirectory property.
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.WorkingDirectory = your_directory
(...)
//do your thing
Process process = Process.Start(processStartInfo);
Arguments can be passed via the Arguments property

How to zip a Folder using SSIS 2008 without using any third party s/w?

I have a scenario where i would like to compress my folder due to presence of large number of files present in them using SSIS 2008. Consider it like i have one Source Folder and one Target Folder and while moving files from "SRC" to "TGT" the folder must be compressed in destination.Now feasible option for doing this i think is SSIS Script task ,since I cannot use Execute Process task due to restriction of using any third party software like 7z/Winrar etc.But i am not able to implement this even after using SSIS Script Component.Tried many online solutions but it did not work.How can i implement such thing using SSIS 2008?
You can use the ZipPackage class if you are targeting .Net 3 and above. Complete example here:
https://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx
There is also a ZipArchive class, example here:
https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx
I did this exercise , I created a Script Task to perform compression of a folder by using the compression provided by Windows;
the folder name can dynamically change.
In this way it is not necessary to use third party software like 7z/Winrar etc..
You need to provide to the Script Task the folder to be zipped and the name of the compressed folder as ReadOnlyVariables (to be added in the tab ReadOnlyVariables)
These two variables must be defined in the Variables tab (String type) of the package and can be changed dynamically through a cycle (eg. for each)
I use these two variables:
sFolderCompressed - the folder '.zip' that you want to obtain eg. \\XX.XX.XX.XX\C$\.....\folderCompressed
sFolderSource - the source folder containing the files affected eg. \\XX.XX.XX.XX\C$\.....\folderSource
(*)
The script is made using c#, choose Script Language: Microsoft Visual C# 2008
This is the code to be added in the Main method:
public void Main()
{
// TODO: Add your code here
try
{
// variables used in process
string l_sFolderCompressed = (string)Dts.Variables["User::sFolderCompressed"].Value;
string l_sFolderSource = (string)Dts.Variables["User::sFolderSource"].Value;
string l_sCommand = "zip -j " + l_sFolderCompressed + " " + l_sFolderSource + "/*";
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/C " as the parameters.
// Incidentally, /C tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/C " + l_sCommand);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Possibly display the command output.
}
catch (Exception objException)
{
Dts.TaskResult = (int)ScriptResults.Failure;
// Log the exception
}
Dts.TaskResult = (int)ScriptResults.Success;
}
You can also manage a single file
"cmd", "/C zip -j c:\\...\file.zip c:\\..\file.txt");
I hope can help

How to execute command-line arguments with parameters in C#?

I'm trying to compare two folders using a third party comparison tool called UltraCompare. The following line calls the program and opens two files... but this doesn't do anything except open them, plus it doesn't work properly for folders.
Process.Start("C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe",
textBoxContents1 + " " + textBoxContents2);
I'd like to use the following command line call which opens two folders, runs a comparison on them, and stores the results in output.txt: uc -d -dmf "c:\dir1" "c:\dir2" -o "c:\output.txt"
Also, I'd need to use variables for the folders instead of hardcoding the paths.
How can I work that into my C# code?
UPDATE 1:
I've modified my code according to your suggestions:
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf \"{0}\" \"{1}\" -o c:\\output2.txt",
textBoxContents1, textBoxContents2);
p.Start();
I'm wondering why the third line containing the arguments still doesn't work...
UPDATE 2:
My mistake. It is working now!! Just doesn't display the folders in UltraCompare but it is still writing and saving the output. Thanks guys!
You can use
yourProcess.StartInfo.Arguments = " .....";
Sample
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\IDM Computer Solutions\\UltraCompare\\uc.exe";
p.StartInfo.Arguments = String.Format("-d -dmf {0} {1} -o c:\output.txt",textBoxContents1, textBoxContents2);
p.Start();
Process.Start(new ProcessStartInfo
{
FileName = #"C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe",
Arguments = String.Format("\"{0}\" \"{1}\"", textBoxContents1, textBoxContents2)
});
Make sure you use quotation marks for arguments, too!
If any of textBoxContents1 or textBoxContents2 contains spaces, you are busted!

How to loop through all dlls of an exe and see if a certain function is exported

I have a application which is a game launcher.
While the application is running I want to loop through the dlls which are loaded in that file and check if a certain function is exported.
How can I do that?
I'm not talking about using net reflector I want to check the exported functions by the dlls loaded in the memory from the game launcher and loop through them to see if a certain function is called.
Jax, look at this StackOverflow question. It should be able to do exactly what you need. To keep things simple, look specifically at the comment that says to use Dumpbin.exe /exports. That will probably be the easiest way to do it. If you absolutely need to do it programmically, look at this Stackoverflow Question instead.
Using the Dumpbin method, you could do something like this:
// The name of the DLL to output exports from
const string dllName = #"C:\Windows\System32\Wdi.dll";
string output = string.Empty;
var info = new ProcessStartInfo();
var process = new Process();
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.EnvironmentVariables.Remove("Path");
// DumpBin requires a path to IDE
info.EnvironmentVariables.Add("Path", Environment.GetEnvironmentVariable("Path") + #";c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\");
// Your path might be different below.
info.FileName = #"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\dumpbin.exe";
info.Arguments = string.Format("/exports \"{0}\"", dllName);
process.OutputDataReceived += (senderObject, args) => output = output + args.Data;
process.StartInfo = info;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
// output now contains the output
Use .net reflection. Here is good sample how to do this:
http://towardsnext.wordpress.com/2008/09/17/listing-types-and-methods-of-assembly-reflection/
You might want to look at something like
Using Reflection to load unreferenced assemblies at runtime in C#
Reflection Examples [C#]

Programmatically setting startin location when starting a process

I have an application that creates a shortcut on my desktop and allows you to drag and drop files into the shortcut to perform an action (convert a word document to PDF). Now what I am trying to do is perform this action programmatically using shellexecute (.NET Process.Start()).
The problem is that it doesnt seem to be working and I have a sneaking suspicion this has something to do with the fact that the shortcut created has the "Start in" parameter set to a specific folder.
So it looks like this:
Shortcut target: "C:\Program Files (x86)\MyPDFConvertor\MyPDFConvertor.exe"
Shortcut startin: "C:\Program Files (x86)\MyPDFConvertor\SomeSubfolder\SomeSubSubFolder"
My code was the following.
System.Diagnostics.Process.Start("C:\\Program Files (x86)\\MyPDFConvertor\\MyPDFConvertor.exe", "C:\\MyFiles\\This is a test word document.docx");
Fundamentally my question boils down to: What does "Startin" actually mean/do for shortcuts and can I replicate this functionality when starting an application using either shellexecute or Process.Start?
When you use Process.Start you can call it with a ProcessStartInfo which in turn happens to be able to setup a WorkingDirectory property - this way you can replicate that behaviour.
As Yahia said, set the WorkingDirectory property. You also need to quote the arguments. Here is a rough example:
//System.Diagnostics.Process.Start("C:\\Program Files (x86)\\MyPDFConvertor\\MyPDFConvertor.exe", "C:\\MyFiles\\This is a test word document.docx");
ProcessStartInfo start = new ProcessStartInfo();
//must exist, and be fully qualified:
start.FileName = Path.GetFullPath("C:\\Program Files (x86)\\MyPDFConvertor\\MyPDFConvertor.exe");
//set working directory:
start.WorkingDirectory = Path.GetFullPath("C:\Program Files (x86)\MyPDFConvertor\SomeSubfolder\SomeSubSubFolder");
//arguments must be quoted:
const char quote = '"';
start.Arguments = quote + "C:\\MyFiles\\This is a test word document.docx" + quote;
//disable the error dialog
start.ErrorDialog = false;
try
{
Process process = Process.Start(start);
if(process == null)
{//started but we don't have access
}
else
{
process.WaitForExit();
int exitCode = process.ExitCode;
}
}
catch
{
Console.WriteLine("failed to start the program.");
}

Categories