here's code:
string command = string.Empty;
DirectoryInfo dInfoForTesting = new DirectoryInfo(#"E:\TestFiles");
FileInfo[] files = dInfoForTesting.GetFiles();
foreach (FileInfo file in files)
{
string rndPswd = "134";
command = "arj a -m1 -g" + rndPswd + " " + file.Name + ".arj " + file.FullName;
Process.Start(#"C:\ARJ_TEST\programming\test\ARJ.EXE", command);
}
I want to create automatically archives protected by simple password 134 uisng ARJ archiver. But when i run this code for the first file i see the image below. But this file exists!
Please, help to understand me, what i am doing wrong.
EDIT:
i 've decided to rewrite code to run cmd in the folder E:\TestFiles, where there are all files for testing:
ProcessStartInfo startInfo = new ProcessStartInfo
{
WorkingDirectory = #"E:\TestFiles",
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = false
};
foreach (FileInfo file in files)
{
string rndPswd = "134";
command = "arj a -m1 -g" + rndPswd + " " + file.Name + ".arj " + file.Name;
startInfo.Arguments = command;
Process.Start(startInfo);
}
For example, if i run command arj a -m1 -g134 10.arj 10 directly from cmd, arj creates an archive 10.arj. But when i run my code for the first file (10) nothing happens...
Related
I need to pass .zip file location into files parameter in the below c# code.
If the file name DOES NOT CONTAIN spaces, everything is working fine. But, if the file name DOES CONTAIN spaces, it is throwing below error.
Cannot find archive
Below is my code: Can anybody please suggest me how can I solve this problem?
static void UnzipToFolder(string zipPath, string extractPath, string[] files)
{
string zipLocation = ConfigurationManager.AppSettings["zipLocation"];
foreach (string file in files)
{
string sourceFileName = string.Empty;
string destinationPath = string.Empty;
var name = Path.GetFileNameWithoutExtension(file);
sourceFileName = Path.Combine(zipPath, file);
destinationPath = Path.Combine(extractPath, name);
var processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = zipLocation;
processStartInfo.Arguments = #"x " + sourceFileName + " -o" + destinationPath;
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
var process = Process.Start(processStartInfo);
process.WaitForExit();
}
}
Add quotes around the file paths:
processStartInfo.Arguments = "x \"" + sourceFileName + "\" -o \"" + destinationPath + "\"";
Or for readability (with C# 6):
processStartInfo.Arguments = $"x \"{sourceFileName}\" -o \"{destinationPath}\"";
All filenames and paths which contain spaces must be quoted.
Next, regarding your question, how about stating the path like:
7z a -tzip C:\abc\zipfilename C:\"Program files"\DirectoryOrFile
I'm trying to run the following:
String command = #"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder;
System.Diagnostics.Process.Start("CMD.exe", "/K PATH C:\\Program Files\\R\\R-3.1.1\\bin;%path%");
System.Diagnostics.Process.Start("CMD.exe", "/K " + command);
Nothing happens when I execute it, does anyone know why? If I try
System.Diagnostics.Process.Start("CMD.exe", "/K MD TEST");
That works fine :s
e: Some extra info, The first command is setting the PATH so that the Rscript can be called by just typing Rscript. Also, both of these commands work when I do them in a normal CMD interface.
Prepare a batch file and execute it
using(StreamWriter sw = new StreamWriter("runscript.cmd", false))
{
sw.WriteLine(#"PATH C:\Program Files\R\R-3.1.1\bin;%path%");
sw.WriteLine(#"Rscript C:\Users\someone\Documents\generate_files.R " + fname + " " + folder);
}
System.Diagnostics.Process.Start("CMD.exe", "/K runscript.cmd");
This assumes that you have read/write permissions on the current directory. You can change the location to a more suitable position using
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileCmd = Path.Combine(path, "runscript.cmd");
using(StreamWriter sw = new StreamWriter(fileCmd, false)
....
As realised by Steve, I was running two console processes. To resolve this I simply ran both commands in the same process.
cmd.Arguments = "/K \"" + fullFilePath;
*try double " " right before PATH
I want to compress a folder into a file with the .7z extension, with 7zip.
I would like to know how I would do this, because I'm not sure (which is why I'd asking.)
This is in C#.
Links to pages or sample code would be helpful.
code to zip or unzip file using 7zip
this code is used to zip a folder
public void CreateZipFolder(string sourceName, string targetName)
{
// this code use for zip a folder
sourceName = #"d:\Data Files"; // folder to be zip
targetName = #"d:\Data Files.zip"; // zip name you can change
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"D:\7-Zip\7z.exe";
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
this code is used to zip a file
public void CreateZip(string sourceName, string targetName)
{
// file name to be zip , you must provide file name with extension
sourceName = #"d:\ipmsg.log";
// targeted file , you can change file name
targetName = #"d:\ipmsg.zip";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"D:\7-Zip\7z.exe";
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
}
this code is used for unzip
public void ExtractFile(string source, string destination)
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
string zPath = #"D:\7-Zip\7zG.exe";
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = zPath;
pro.Arguments = "x \"" + source + "\" -o" + destination;
Process x = Process.Start(pro);
x.WaitForExit();
}
catch (System.Exception Ex) { }
}
I agree that this is a duplicate, but I've used the demo from this codeproject before and it is very helpful:
http://www.codeproject.com/Articles/27148/C-NET-Interface-for-7-Zip-Archive-DLLs
Scroll down the page for the demo and good luck!
Here fileDirPath is the path to my folder which has all my files and preferredPath is the path where I want my .zip file to be.
eg:
var fileDirePath = #“C:\Temp”;
var prefferedPath = #“C:\Output\results.zip”;
private void CreateZipFile(string fileDirPath, string prefferedPath)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = #"C:\Program Files\7-Zip\7z.exe";
p.Arguments = "a \"" + prefferedPath + "\" \"" + fileDirPath + "\"";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
return;
}
I have a strange requirement. User can upload their video of any format (or a limited format). We have to store them and convert them to .mp4 format so we can play that in our site.
Same requirement also for audio files.
I have googled but I can't get any proper idea. Any help or suggestions....??
Thanks in advance
You can convert almost any video/audio user files to mp4/mp3 with FFMpeg command line utility. From .NET it can be called using wrapper library like Video Converter for .NET (this one is nice because everything is packed into one DLL):
(new NReco.VideoConverter.FFMpegConverter()).ConvertMedia(pathToVideoFile, pathToOutputMp4File, Formats.mp4)
Note that video conversion requires significant CPU resources; it's good idea to run it in background.
Your Answer
please Replace .flv to .mp4 you will get your answer
private bool ReturnVideo(string fileName)
{
string html = string.Empty;
//rename if file already exists
int j = 0;
string AppPath;
string inputPath;
string outputPath;
string imgpath;
AppPath = Request.PhysicalApplicationPath;
//Get the application path
inputPath = AppPath + "OriginalVideo";
//Path of the original file
outputPath = AppPath + "ConvertVideo";
//Path of the converted file
imgpath = AppPath + "Thumbs";
//Path of the preview file
string filepath = Server.MapPath("~/OriginalVideo/" + fileName);
while (File.Exists(filepath))
{
j = j + 1;
int dotPos = fileName.LastIndexOf(".");
string namewithoutext = fileName.Substring(0, dotPos);
string ext = fileName.Substring(dotPos + 1);
fileName = namewithoutext + j + "." + ext;
filepath = Server.MapPath("~/OriginalVideo/" + fileName);
}
try
{
this.fileuploadImageVideo.SaveAs(filepath);
}
catch
{
return false;
}
string outPutFile;
outPutFile = "~/OriginalVideo/" + fileName;
int i = this.fileuploadImageVideo.PostedFile.ContentLength;
System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));
while (a.Exists == false)
{
}
long b = a.Length;
while (i != b)
{
}
string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";
ConvertNow(cmd);
string imgargs = " -i \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";
ConvertNow(imgargs);
return true;
}
private void ConvertNow(string cmd)
{
string exepath;
string AppPath = Request.PhysicalApplicationPath;
//Get the application path
exepath = AppPath + "ffmpeg.exe";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = exepath;
//Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"
proc.StartInfo.Arguments = cmd;
//The command which will be executed
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
while (proc.HasExited == false)
{
}
}
protected void btn_Submit_Click(object sender, EventArgs e)
{
ReturnVideo(this.fileuploadImageVideo.FileName.ToString());
}
I know it's a bit old thread but if I get here other people see this too. You shoudn't use it process info to start ffmpeg. It is a lot to do with it. Xabe.FFmpeg you could do this by running just
await Conversion.Convert("inputfile.mkv", "file.mp4").Start()
This is one of easiest usage. This library provide fluent API to FFmpeg.
Is there a way to read each line of 2 multi-line text box? In textBox1 I have as a multi-line String containing a list of compressed files using the following code:
DirectoryInfo getExpandDLL = new DirectoryInfo(showExpandPath);
FileInfo[] expandDLL = getExpandDLL.GetFiles("*.dl_");
foreach (FileInfo listExpandDLL in expandDLL)
{
textBox1.AppendText(listExpandDLL + Environment.NewLine);
}
At the moment part of my code is this:
textBox2.Text = textBox1.Text.Replace("dl_", "dll");
string cmdLine = textDir.Text + "\\" + textBox1.Text + " " + textDir.Text + "\\" + textBox2.Text;
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "expand.exe";
startInfo.UseShellExecute = true;
startInfo.Arguments = cmdLine.Replace(Environment.NewLine, string.Empty);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
The code above takes the name of the compressed file in textBox1 and renames it in textBox2 then runs expand.exe to expand the compressed file. The code basically gives expand.exe the following command as an example:
c:\users\nigel\desktop\file.dl_ c:\users\nigel\desktop\file.dll
it works great if the folder contains only one line of text in textBox1. With multi-lines of text the command is basically:
c:\users\nigel\desktop\loadsoffiles.dl_ etc and doesnt work!
Is there a way to read each line of textBox1, change the string and put it into textBox2 then pass the command to expand.exe?
string cmdLine = textDir.Text + "\\" + lineOFtextBox1 + " " + textDir.Text + "\\" + lineOftextBox2;
EDIT: Just to be clear: TextBox1 contains:
somefile.dl_
someMore.dl_
evenmore.dl_
as a mulitline. My code takes that multiline text and puts it in textBox2 so it contains:
somefile.dll
someMore.dll
evenmore.dll
is there a way to read each line /get each line of textBox1 and textBox2 and do 'stuff' with it?
thank you!
What you need to do is loop over an array of strings, instead of working with a single string. Note that TextBox has a "Lines" property to give you the lines already split into an array
foreach(string line in textBox1.Lines)
{
//your code, but working with 'line' - one at a time
}
So i think the your full solution would be:
foreach (string line in textBox1.Lines)
{
string cmdLine = textDir.Text + "\\" + line + " " + textDir.Text + "\\" + line.Replace("dl_", "dll");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "expand.exe",
Arguments = cmdLine.Replace(Environment.NewLine, string.Empty),
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = true
}
};
process.Start();
process.WaitForExit();
}
Note that we're launching one process for every line in your textbox, which I think is the correct behaviour
First Google hit tells us the following:
string txt = TextBox1.Text;
string[] lst = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
You can try with this code
string a = txtMulti.Text;
string[] delimiter = {Environment.NewLine};
string[] b = a.Split(delimiter, StringSplitOptions.None);