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);
Related
I am trying to open my .exe "application" with 2 arguments. When I write the command line myself without " " it don't want to work. When I write the command line using "C:/Path" "A" "B" it does work.
How do I fix this?
if(element.SaveType == "1")
{
DirectoryInfo srcDir = new DirectoryInfo((string)element.SourcePath);
DirectoryInfo dstDir = new DirectoryInfo((string)element.DestinationPath);
if (element.didEncrypt == true)
{
if (Directory.GetFiles(srcDir.FullName, ((string)element.EncryptExt)).Length == 0)
{
string pat = System.IO.Path.Combine(#"C:\Users\Client Fractal\source\repos\CryptoSoft\CryptoSoft\bin\Debug\netcoreapp3.0\CryptoSoft.exe");
Process p = new Process();
p.StartInfo.FileName = pat;
p.StartInfo.Arguments = srcDir.FullName + dstDir.FullName; // source / target;
Console.WriteLine(p.StartInfo.Arguments);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
}
}
}
Command-line arguments are space-character delimited, so you'd need to insert a space character or your arguments will end up getting squashed together.
p.StartInfo.Arguments = srcDir.FullName + " " + dstDir.FullName;
But of course, if either srcDir.FullName or dstDir.FullName contain space characters of their own (and FullName suggests they do), you will need to surround them with double quote characters.
p.StartInfo.Arguments = "\"" srcDir.FullName + "\" \"" + dstDir.FullName + "\"";
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...
I am making a C# program that will make input file an .iso file.
For example if we write C:\Users\User\Desktpo\a.txt in the textbox that I created and give a destination path for example C:\ it has to create an iso file name a in C:\.
So I downloaded PowerISO and learn about piso.exe then I made some other research about using Process.Start(); in C# so I write these lines of code:
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "\"C:\PowerISO\piso.exe\"";
process.StartInfo.Arguments = str;
process.Start();
But this doesn't work.
Why?
EDIT: I am making an winform using Visual Studio 2017.
Please Note that
C:\PowerISO\piso.exe\
is not a valid C# path, the "\" before the P and the p is an escape indicater.
Either Add verbatim operator "#" before or escape it properly
edit:
In addition, you are adding extra double quotes to the FileName property.
You can check if the path is correct by asserting the file path with File.Exists method
Console.WriteLine(File.Exists(#"C:\PowerISO\piso.exe") ? "File exists." : "File does not exist.");
e.g
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = #"C:\PowerISO\piso.exe";
process.StartInfo.Arguments = str;
process.Start();
edit:
Verbatim operator will work, but escaping is better in my opinion.
string str = " create -o " + TextBox1.Text + ".iso -add " + TextBox2.Text + "//" ;
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "C:\\PowerISO\\piso.exe";
process.StartInfo.Arguments = str;
process.Start();
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 am building a gui for a commandline program.
In txtBoxUrls[TextBox] file paths are entered line by line.
If the file path contains spaces the program is not working properly.
The program is given below.
string[] urls = txtBoxUrls.Text.ToString().Split(new char[] { '\n', '\r' });
string s1;
string text;
foreach (string s in urls)
{
if (s.Contains(" "))
{
s1 = #"""" + s + #"""";
text += s1 + " ";
}
else
{
text += s + " ";
}
}
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = #"wk.exe";
proc.StartInfo.Arguments = text + " " + txtFileName.Text;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
//Get program output
string strOutput = proc.StandardOutput.ReadToEnd();
//Wait for process to finish
proc.WaitForExit();
For example if the file path entered in txtBoxUrls is "C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm", The program will not work. This file path with double quotes will work in the windows command line(I am not using the GUI) nicely.
What would be the solution.
proc.StartInfo.Arguments = text + " " + txtBoxUrls.Text + " " + txtFileName.Text;
In this line, text already contains the properly quoted version of your txtBoxUrls strings. Why do you add them again in unquoted form (+ txtBoxUrls.Text)? If I understood your code corrently, the following should work:
proc.StartInfo.Arguments = text + " " + txtFileName.Text;
In fact, since txtFileName.Text could probably contain spaces, you should quote it as well, just to be sure:
proc.StartInfo.Arguments = text + " \"" + txtFileName.Text + "\"";
(or, using your syntax:)
proc.StartInfo.Arguments = text + #" """ + txtFileName.Text + #"""";
Usually to get around spaces in filenames, you'll need to wrap your argument in double quotes. If you leave out the quotes the program will think it has two arguments. Something like this...
wk.exe "C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm"
Also, this line appears to have too many quotes. Four, instead of three:
s1 = #"""" + s + #"""";
Take a look at the Path class - http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Path.combine might be what you are looking for.