Having a weird problem. I have this C# snippet of code I am using to a extract self-extracting .exe on a CentOS 6.5 machine with Mono:
try
{
//new bash process
Process bash = new Process();
bash.StartInfo.FileName = "/bin/bash";
string command = #"unzip " + Globals.fiberDirectory + #"""" + file + ".exe" + #"""";
//Check command string
Console.WriteLine(command);
bash.StartInfo.Arguments = "-c \" " + command + " \"";
bash.StartInfo.UseShellExecute = false;
bash.Start();
}
catch (Exception e)
{
Console.WriteLine(" ...Not Loaded! - " + e.Message);
}
finally
{
}
It outputs this:
unzip "/home/pan/maps/data/telus/FVData/Fiber/5656 all.exe"
unzip: cannot find or open /home/pan/maps/data/telus/FVData/Fiber/5656, /home/pan/maps/data/telus/FVData/Fiber/5656.zip or /home/pan/maps/data/telus/FVData/Fiber/5656.ZIP.
when I run this command from my shell manually:
unzip /home/pan/maps/data/telus/FVData/Fiber/"5656 all.exe"
It works perfectly. Any ideas?
Related
I want to make a Windows service that will monitor a path for a file. When one appears, it reads it and runs the program in the file. The structure of the command files is delay;path;filename;arguments. When testing this as a winforms app, it works great. But when I moved it to a service, it dies saying it cannot find the file.
I tried changing the service to run as my own AD account. I also tried adding code to log to the eventlog whether or not it can see the command file and the executable the command file should be running. While I know it's a terrible idea for production, just to test I set all related directories and files to have everyone:f NTFS permissions, but to no avail.
void onCreation(object sender, FileSystemEventArgs e) {
try {
Thread.Sleep(100);
if (File.Exists(e.FullPath)) {
log("Located cmd file [" + e.FullPath + "]", EventLogEntryType.Information);
} else {
log("Couldn't find cmd file [" + e.FullPath + "]", EventLogEntryType.Information);
}
string[] cmd = File.ReadAllText(e.FullPath).Split(';');
Process proc = new Process();
proc.StartInfo.WorkingDirectory = cmd[1];
proc.StartInfo.FileName = cmd[2];
proc.StartInfo.Arguments = cmd[3];
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
Dictionary<string, object> processData = new Dictionary<string, object>(){{"delay",cmd[0]},{"proc", proc},{"file",e.FullPath}};
Thread tmp = new Thread(new ParameterizedThreadStart(processThread));
tmp.Start(processData);
} catch (Exception ex) {
log("Error during onCreation: " + ex.Message, EventLogEntryType.Error);
}
}
void processThread(object procdata) {
Dictionary<string, object> processData = (Dictionary<string, object>)procdata;
try {
int delay = System.Convert.ToInt16(processData["delay"]);
Thread.Sleep(delay);
Process p = (Process)processData["proc"];
if (!File.Exists(p.StartInfo.WorkingDirectory + "\\" + p.StartInfo.FileName)) {
log("Couldn't find proc file [" + p.StartInfo.WorkingDirectory + "\\" + p.StartInfo.FileName + "]", EventLogEntryType.Information);
} else {
log("Located proc file [" + p.StartInfo.WorkingDirectory + "\\" + p.StartInfo.FileName + "]", EventLogEntryType.Information);
}
p.Start();
try {
File.Delete(processData["file"].ToString());
} catch (Exception ex) {
log("** Exception clearing file [" + processData["file"].ToString() + "]: " + ex.Message, EventLogEntryType.Warning, true);
}
} catch (Exception ex) {
log("Error during processThread: " + ex.Message, EventLogEntryType.Error);
log("Process data: [" + processData["delay"].ToString() + "][" + processData["file"].ToString() + "][" + ((Process)processData["proc"]).StartInfo.WorkingDirectory + "][" + ((Process)processData["proc"]).StartInfo.FileName + "][" + ((Process)processData["proc"]).StartInfo.Arguments + "]", EventLogEntryType.Information);
}
}
I have my config setting the listenpath to d:\misc\sites----.com\cmds then I say "Alexa, trigger fan test" and my web server generates a file in that folder like: 0;d:\misc\iot;wemo.exe;10.0.0.72 on; for it to process. It sees it and I get the following eventlog entries:
Located cmd file [d:\sites\----\www\cmds\15659086948907.dat]
Located proc file [d:\misc\iot\wemo.exe]
Error during processThread: The system cannot find the file specified
Process data: [d:\sites\----\www\cmds\15659086948907.dat][d:\misc\iot][wemo.exe][10.0.0.72 off]
What is it about being in a service that is making it fail? Thanks!
I am trying to install a virtual printer (for all versions of Windows) with a Windows desktop application. I want this printer to be available in the drop down list in the print dialog (especially from Office).
I am trying with this code:
public static void installPrinter(string printerName)
{
string arg;
arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + #" /f C:\Windows\inf\ntprint.inf /r " + "\"" + "lpt1:" + "\"" + " /m " + "\"" + "Brother DCP-116C" + "\""; //initial args
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "rundll32.exe";
p.Arguments = arg;
p.WindowStyle = ProcessWindowStyle.Hidden;
try
{
Process.Start(p);
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
}
but it doesn't work. Any suggestions?
After a lot of research I made it work on win 7,8,8.1,10 on both x84 and x64 platforms (without any embeded drivers).
Here is the code:
public static void installPrinter(string printerName) //works on win 7,8,8.1,10 on both x84 and x64
{
//https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32-printui
// /if Installs a printer by using an .inf file.
// /b[name] Specifies the base printer name.
// /#[file] Specifies a command-line argument file and directly inserts the text in that file into the command line.
// /f[file] Species the Universal Naming Convention (UNC) path and name of the .inf file name or the output file name, depending on the task that you are performing. Use /F[file] to specify a dependent .inf file.
// /r[port] Specifies the port name.
// /m[model] Specifies the driver model name. (This value can be specified in the .inf file.)
string arg;
arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + #" /f C:\Windows\inf\ntprint.inf /r " + "\"" + "lpt1:" + "\"" + " /m " + "\"" + "Generic / Text Only" + "\"";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "rundll32.exe";
p.Arguments = arg;
p.WindowStyle = ProcessWindowStyle.Hidden;
try
{
Process.Start(p);
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
}
I have checked and there are many posts similar but I cannot solve this. This is a SQL CLR proc to run an exe that parses PDF files. I can run same code as myself in console app. Permissions are not an issue. I am dumping to event log and the ProcessStartInfo is full formed. UseShellExecute - I am not certain. I have also created a .bat file to execute the code - run it manually - it runs fine. Via this CLR program ? Hangs or runs right away but does nothing. At wits end. My goal was to create the CLR using the Nuget from itext but I had a real hard time installing. SQL CLR doesnt support it but I tried anyway.
Thanks in advance.
[Microsoft.SqlServer.Server.SqlProcedure]
public static void PDFToCSVViaExe_CLR()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
string args = #"\\app\Systems\Universal";
args = " \"" + args + "\"" + " \"" + "*" + "\"" + " \"" + "LVM" + "\"";
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = "c:\\SqlCLR\\PDFRipper\\PDFToCSV.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = args;
EventLog.WriteEntry(".NET Runtime", startInfo.FileName.ToString()+" " + args, EventLogEntryType.Warning, 1000);
string folder = "C:\\SqlCLR\\PDFRipper\\LVM.bat";
try
{
//using (new ImpersonationNamespace.Impersonation("domain", "user", "password"))
{
EventLog.WriteEntry(".NET Runtime", System.Security.Principal.WindowsIdentity.GetCurrent().Name + " OR " + Environment.UserName, EventLogEntryType.Warning, 1000);
folder = System.IO.Path.GetDirectoryName(folder);
if (!Directory.Exists(folder))
{
throw (new Exception("Directory does not exist for " + folder));
}
else
{ EventLog.WriteEntry(".NET Runtime", folder + " exists.", EventLogEntryType.Warning, 1000); }
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
catch (Exception ex)
{
using (EventLog EventLog = new EventLog("Application"))
{
EventLog.Source = "Application";
//eventLog.WriteEntry(ex.Message);
EventLog.WriteEntry(".NET Runtime", ex.Message + "Trace" + ex.StackTrace, EventLogEntryType.Warning, 1000);
//eventLog.WriteEntry(".NET Runtime", ex.Message, EventLogEntryType.Warning, 1000);
}
}
I have a .bat file on the remote machine. when i run manually cmd as admin, executed psexec works correctly. But in C#, i stuck somewhere. I would like to run this .bat file as administrator in C# code. Here is my code. Could someone help me, please?
public void CopyAtoBFolder()
{
int waitForExit = 1000 * 300;
string localDestinationFolderPath = #"D:\Tool\";
string call = localDestinationFolderPath + "CopyAtoBFolder.bat";
Process process = new Process();
process.StartInfo.FileName = this.path + #"\PsExec.exe";
process.StartInfo.Arguments = "-accepteula " + localDestinationFolderPath + #" \\" + ip + " -i -u " + username + " -p " + password + " " + call;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.BeginOutputReadLine();
var ended = process.WaitForExit(waitForExit);
if (!ended)
{
throw new TifPcbaException("Process timed out.");
}
int resultReturnCode = process.ExitCode;
if (resultReturnCode != 0)
{
throw new TifPcbaException("PSExec return code " + resultReturnCode + " shows an error calling " + call + Environment.NewLine);
}
}
And here is the error.
PSExec return code 6 shows an error calling D:\Tool\CopyAtoBFolder.bat
Add a app.manifest in your project and change the following line in app.manifest file:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
This would force your application to run as admin
The situation:
I want to modify the folder quotas on my FileServer through a process executing dirquota.exe
The Problem:
The Process being executed gives no result at all
So Far:
I've redirected the process and arguments being executed on my FileServer to take a closer look what's happening exactly on the serverside.
The executed process gave no exception and everything went just fine, it seemed..
When looking at the current folder quota's on my FileServer nothing has changed..I decided to copy paste my arguments in a CMD.exe on the server, then it all went fine...
I cannot figure why it is not working on my FileServer, probably somthing simple but I need some help here
Important Info:
I'm installing a Windows Service on my FileServer and calling the Method through SOUPUI (This is all working fine).
The installed service is running as a Domain admin and has all the required rights to perform these actions
The Class
public class Quota
{
public string FolderLocation;
public int SizeInMB;
public string FileServerName;
}
The Method
public string SetFolderQuota(Quota quota)
{
Process QuotaProcess = new Process();
QuotaProcess.StartInfo.RedirectStandardOutput = false;
QuotaProcess.StartInfo.FileName = #"cmd.exe";
QuotaProcess.StartInfo.UseShellExecute = true;
QuotaProcess.StartInfo.Arguments = "/C " + "dirquota Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName;
try
{
QuotaProcess.Start();
}
catch(Exception Ex)
{
return Ex.Message;
}
return "Correctly Executed: " + QuotaProcess.StartInfo.FileName + QuotaProcess.StartInfo.Arguments;
}
Found The Problem
dirquota.exe is redirected using Windows-on Windows 64-bit redirection. What's happening is that my launch request (from a 32-bit process) is being redirected to %windir%\SysWOW64\dirquota.exe. Since there's no 32-bit version of this particular executable on 64-bit installs, the launch fails. To bypass this process and allow my 32-bit process to access the native (64-bit) path, I have to reference %windir%\sysnative instead
The Code
public string SetFolderQuota(Quota quota)
{
string FileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),#"sysnative\dirquota.exe");
Process QuotaProcess = new Process();
QuotaProcess.StartInfo.RedirectStandardOutput = false;
QuotaProcess.StartInfo.FileName = FileLocation;
QuotaProcess.StartInfo.UseShellExecute = true;
QuotaProcess.StartInfo.Arguments = " Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName;
try
{
QuotaProcess.Start();
}
catch(Exception Ex)
{
return Ex.Message + Environment.NewLine + "FileLocation: " + FileLocation;
}
return "Correctly Executed: " + QuotaProcess.StartInfo.FileName + QuotaProcess.StartInfo.Arguments;
}
Best if you can redirect the output of Process to a log file and see what is the actual exception..
ProcessStartInfo process = new ProcessStartInfo
{
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = #"cmd.exe",
Arguments = "/C " + "dirquota Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName
};
Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();
Log the value of output to get the exact exception caused by execution of this command