public string DecryptFile(string encryptedFilePath)
{
const string quote = "\"";
FileInfo info = new FileInfo(encryptedFilePath);
string decryptedFileName = info.FullName.Substring(0, info.FullName.IndexOf(".pgp") + 4);
//decryptedFileName = info.FullName.Replace(".pgp", "");
decryptedFileName = decryptedFileName.Replace(this.txtFolderPath, this.txtFolderPath + "\\ProcessedZip\\");
//decryptedFileName = "C:\\pgpTest\\ProcessedZip\\"+ info.Name;
decryptedFileName = decryptedFileName.Replace(".pgp", "");
decryptedFileName = quote + decryptedFileName + quote;
string encryptedFileName = quote + info.FullName + quote;
string password = "thisisnottherealpassword";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = this.txtFolderPath;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(psi);
string sCommandLine = "gpg.exe --pinentry-mode=loopback --passphrase \"" + password + "\" -d -o --batch --verbose --yes --output " + decryptedFileName + #" --decrypt " + encryptedFileName;
process.StandardInput.WriteLine(sCommandLine);
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
//string result = process.StandardOutput.ReadToEnd();
//string error = process.StandardError.ReadToEnd();
process.Close();
return decryptedFileName;
}
The above code works in a separate project but having it utilized by another layer of the application through binaries is not spitting out a decrypted file. I'm not sure why as no errors seem to be thrown and using the commandline string in my own command line will correctly run the process.
Any thoughts on why this could be?
Related
I am running doors.exe using Process class. When I run it in the console application, there is no problem, but the program hangs in the web application. What could be the reason for this?
here is my code below;
public string ExecuteDoorsScript(string dxlScriptPath, string userName, string password)
{
//Start the process
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = #"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe";
info.Arguments = "-b \"" + dxlScriptPath + "\" -u " + userName + " -P " + password + "-W -d 1234567#databasepath.it";
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
var process = Process.Start(info);
//Wait for the window to finish loading.
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
//Wait for the process to end.
process.WaitForExit();
if (!String.IsNullOrEmpty(error))
{
throw new Exception("Doors run dxl error: " + error);
}
return output;
}
my dxl script;
string topfoldername = "/Test"
void listIndices(Folder parent, int level)
{
Item i
Folder f
for i in parent do {
f = folder(i)
cout << (uniqueID i) ";" (name i) ";" ((type i)) ";" "\n"
}}
listIndices(folder topfoldername, 1)
I tried to call a .exe program I have calling cmd from a c# script that generates a command. When I run the code I get the output the process tried to write to a nonexistent pipe. But when I run said command using a cmd open from Windows it runs perfectly. What I am doing wrong? I am using Visual Studio 2022.
SilentMkdir(baseDir + "/00_CameraInit");
string binName =pre + binDir + "\\aliceVision_cameraInit.exe";
string dstDir =pre + baseDir + "/00_CameraInit\\";
string cmdLine = binName;
cmdLine = cmdLine + " --defaultFieldOfView 45.0 --verboseLevel info --sensorDatabase \"\" --allowSingleView 1";
cmdLine = cmdLine + " --imageFolder \"" + pre + srcImageDir + "\"";
cmdLine = cmdLine + " --output \"" + dstDir + "cameraInit.sfm\"";
Console.WriteLine(cmdLine);
var processInfo = new ProcessStartInfo("cmd.exe", cmdLine);
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.CreateNoWindow = false;
//processInfo.RedirectStandardOutput = !string.IsNullOrEmpty("output.txt");
int exitCode = -1;
string output = null;
try
{
var process = Process.Start(processInfo);
process.Start();
output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
process.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine("Run error" + e.ToString()); // or throw new Exception
}
I know there are other posts on the matter but none have presented me with a working solution.
I'm trying to take ownership of files under C:\Windows\Media so I can replace them with my own. Currently I've been trying to do that by running either takeown or icacls in CMD, but neither seem to work. The takeown method actually returns success from CMD but I still get access denied when trying to delete the file. With the icacls method I get "the handle is invalid" and still get access denied.
The program is running as administator fyi.
And also in the icacls method changing Environment.Username to "Administrators" doesn't change anything.
Any help is appreciated!
Here is the code for the takeown method:
string[] soundFiles;
soundFiles = Directory.GetFiles(#"C:/Windows/Media", "*.wav");
//string cmdargs = #"/c takeown /F C:\Windows\Media";
string shortcmdargs = #"/c takeown /F ";
//Process.Start("CMD.exe", cmdargs);
string output = "null";
foreach(string file in soundFiles)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = shortcmdargs + #"C:\Windows\Media\" + Path.GetFileName(file);
p.Start();
output = p.StandardOutput.ReadToEnd();
System.IO.File.Delete(file);
System.IO.File.Copy("neco.wav", file);
}
catch(Exception exce)
{
MessageBox.Show("Output: " + output + "\neException: " + exce);
}
}
And here is what I get when running it:
Here is the code for the icacls method:
string[] soundFiles;
soundFiles = Directory.GetFiles(#"C:/Windows/Media", "*.wav");
string cmdargs = #"/c takeown /F C:\Windows\Media";
string shortcmdargs = #"/c takeown /F ";
Process.Start("CMD.exe", cmdargs);
string output = "null";
foreach(string file in soundFiles)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"/c icacls " + #"C:\Windows\Media\" + Path.GetFileName(file) + " /grant " + Environment.UserName + ":(OI)(CI)F /T";
p.Start();
output = p.StandardOutput.ReadToEnd();
System.IO.File.Delete(file);
System.IO.File.Copy("neco.wav", file);
}
catch(Exception exce)
{
MessageBox.Show("Output: " + output + "\neException: " + exce);
}
}
And here is what I get with it:
I am tryin got execute a msdeploy.exe command using cmd from visual studio with c# as scripting language
string filename = #"C:\Deploy\Test\Test.zip";
string servername = #"PADEVSPTAPP";
string compname = #"IIS Web Application Name";
string appvalue = #"Test";
string strCmdText;
strCmdText = "msdeploy.exe -verb:sync -source:package=" + filename + " -dest=auto,computerName=" + servername + " -setParam=name=" + compname + ",value=" + appvalue + " -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + strCmdText);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException.ToString());
}
the string outcome is
msdeploy.exe -verb:sync -source:package="C:\\Deploy\\Test\\Test.zip"
-dest=auto,computerName="PADEVSPTAPP" -setParam=name="IIS Web Application Name",value="Test" -allowUntrusted
but this does not work due to \\ in the command.
How should i execute this command.
I even tried with powershell script ,which also did not work
string PS_script = #"$msdeploy = ""C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe""
$package = """;
PS_script = PS_script + Row.deployfile;
PS_script = PS_script + #"""
$compname = ""PADEVSPTAPP""
$appname = ""IIS Web Application Name""
$appvalue = """;
PS_script = PS_script + changetype[0];
PS_script = PS_script + #"""
$md = $(""`""{0}`"" -verb:sync -source:package=`""{1}`"" -dest=auto,ComputerName=`""{2}`"" -setParam=name=`""{3}`"",value=`""{4}`"" -allowUntrusted"" -f $msdeploy, $package, $compname, $appname, $appvalue)
cmd.exe /C ""`""$md`""""";
I have no clue where I am going wrong.
You are using an equals sign where it should be colon.
It's supposed to be -dest: and not -dest=
Same with setParam, it's supposed to be -setParam: not -setParam=
I suspect you don't actually have double backslashes \\ in your string it will just look like that if you inspect via the debugger - I suspect thats whats throwing you off.
Since you have spaces in your compname variable you need double quotes in your arguments string (probably around all your variables would be a good idea).
Also try running msdeploy.exe directly instead of via cmd.exe /c.
I assumed your msdeploy.exe is located in C:\Program Files (x86)\IIS\Microsoft Web Deploy V3
The string outcome is:
-verb:sync -source:package="C:\Deploy\Test\Test.zip" -dest:auto,computerName="PADEVSPTAPP" -setParam:name="IIS Web Application Name",value="Test" -allowUntrusted
Put it all together:
string filename = #"C:\Deploy\Test\Test.zip";
string servername = #"PADEVSPTAPP";
string compname = #"IIS Web Application Name";
string appvalue = #"Test";
string strCmdText;
strCmdText = "-verb:sync -source:package=\"" + filename + "\" -dest:auto,computerName=\"" + servername + "\" -setParam:name=\"" + compname + "\",value=\"" + appvalue + "\" -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo(#"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe");
procStartInfo.Arguments = strCmdText;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(result);
}
catch (Exception objException)
{
Console.WriteLine(objException.ToString());
}
BONUS INFO
If you need a more bulletproof way of determining where msdeploy.exe is located maybe have a look at these links:
https://gist.github.com/SergeyAxenov/15cf008531e6d0741533
How to find out what version of webdeploy/msdeploy is currently installed?
I have requirement to execute the command line arguments. If file path contains the Spaces it doesn’t work properly. It returns the error file not found. The program is given below.
public void Method()
{
string docFile = #"C:\Test Document1.doc";
string docxFile = #"C:\Test Document1.docx";
string file = #"C:\doc2x_r649 (1)\doc2x_r649\doc2x.exe";
ExecuteCommand(file, string.Format(docFile + " -o " + docxFile));
}
public static string ExecuteCommand(string file, string command)
{
String result;
try
{
//Create a new ProcessStartInfo
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
//Settings
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.FileName = file;
procStartInfo.Arguments = command;
//Create new Process
System.Diagnostics.Process proc = new System.Diagnostics.Process();
//Set ProcessStartInfo
proc.StartInfo = procStartInfo;
//Start Process
proc.Start();
//Wait to exit
proc.WaitForExit();
//Get Result
result = proc.StandardOutput.ReadToEnd();
//Return
return result;
}
catch
{
}
return null;
}
If file path doesn't contains spaces it works properly.
Have you tried adding quotes to your paths?
ExecuteCommand(file, string.Format("\"" + docFile + "\" -o \"" + docxFile + "\""));
Try this
ExecuteCommand(file, string.Format("\"{0}\" -o \"{1}\"",docFile , docxFile));