Hey i am trying to launch my Application.exe as a runas administrator but still not able to launch i have tried both verb="runas" as well as created app.manifest file but my exe not opening in administrator mode.
I am using this code :
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = GetASDeploymentExe(),
Arguments = GetASDeploymentCmdArgs(),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
Domain = "***",
UserName = username,
Password = secstr,
Verb = "runas"
}
};
proc.Start();
config.ASDeploymentToolPID = proc.Id;
string line = string.Empty;
do
{
line = proc.StandardOutput.ReadLine();
Console.WriteLine(line);
} while (!proc.HasExited);
string error = string.Empty;
while (!proc.StandardError.EndOfStream)
{
error = proc.StandardError.ReadLine();
Logger.WriteError(error);
}
}
Try this:
//Vista or higher check
if (System.Environment.OSVersion.Version.Major >= 6)
{
p.StartInfo.Verb = "runas";
}
Alternatively, go the manifest route for your application.
Related
I tried to open a OpenVPN connection using a small c# program.
Below is the code I used.
static void Main(string[] args)
{
Process rs = new Process();
var netCredential = new System.Net.NetworkCredential("User", "PWD", "Domain");
System.Environment.CurrentDirectory = ".\\";
ProcessStartInfo info = new ProcessStartInfo
{
FileName = "c:\\programme\\openvpn\\bin\\openvpn.exe",
Arguments = "--config c:\\programme\\openvpn\\config\\NAS-Name.ovpn",
UserName = netCredential.UserName,
Domain = netCredential.Domain,
Password = netCredential.SecurePassword,
UseShellExecute = false,
//RedirectStandardError = true,
//RedirectStandardOutput = true,
//CreateNoWindow = true,
WorkingDirectory = Path.GetDirectoryName("c:\\programme\\openvpn\\bin\\openvpn.exe")
};
var p = Process.Start(info);
}
This code does work, but only on the computer were I compiled it.
On our server (Win server 2019) I get the error:
Unhandled Exception: System.ComponentModel.Win32Exception:System cant find file
at System.Diagnostics.ProcessWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at ConnectOpenVPN.Program.Main(String[] args) in
C:\User\path\to\Program.cs:Zeile 43
I donĀ“t understand where the last line of the error message comes from.
I assume you are using a german windows or similar.
Beware that the display value of file pathes in windows explorer does not reflect the real path, see picture.
C:\Programme -> C:\Program Files
You can reveal the real path by clicking in the address bar.
try:
static void Main(string[] args)
{
Process rs = new Process();
var netCredential = new System.Net.NetworkCredential("User", "PWD", "Domain");
Environment.CurrentDirectory = ".\\";
var vpnConfigPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"openvpn", "config", "NAS-Name.ovpn");
var vpnPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
#"openvpn","bin","openvpn.exe");
ProcessStartInfo info = new ProcessStartInfo
{
FileName = vpnPath,
Arguments = $"--config \"{vpnConfigPath}\"",
UserName = netCredential.UserName,
Domain = netCredential.Domain,
Password = netCredential.SecurePassword,
UseShellExecute = false,
//RedirectStandardError = true,
//RedirectStandardOutput = true,
//CreateNoWindow = true,
WorkingDirectory = Path.GetDirectoryName(vpnPath)
};
var p = Process.Start(info);
}
string fileName = Server.MapPath(Url.Content("~/CPRDetailsText/CPRDetails2017.txt"));
// Check if file already exists. If yes, delete it.
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
string fileLoc = Server.MapPath(Url.Content("~/CardReader/SDKReadCard.exe"));
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileLoc,
Arguments = "WScript.Shell",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
// proc.Close();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
proc.Close();
By using above code I want to fetch the output of the so its working fine on local machine but when I published to the iis server its hang the website and iis server
Is there a way to display the windows popup msg by using C#?
I mean by using the windows msg.exe program that can be used in cmd, for example:" msg * Hello "
PD: I know that i can use MessageBox.Show() instead. But i want to know if this is possible :(
I wrote 2 ways to do it but none worked:
Process.Start("cmd.exe","/C msg * Hello");
and...
Process cmd = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C msg * Hello",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
}
};
cmd.Start();
Did you try adding msg.exe directly?
Process cmd = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"msg.exe",
Arguments = #"* /v Hello",
WorkingDirectory = Environment.SystemDirectory;
WindowStyle = ProcessWindowStyle.Normal
}
};
cmd.Start();
I encountered the same problem.
This was because the project was configured as "AnyCPU" but the "Prefer 32-bit" option was checked in the "Build" tab of the project configuration. Uncheck that option and the problem will disappear.
Edit: personnaly, I use the following function to locate the binary according to the executable and OS platform (32/64 bits):
public static bool LocateMsgExe(out string returnedMsgPath)
{
returnedMsgPath = null;
string[] msgPaths = new string[] { Environment.ExpandEnvironmentVariables(#"%windir%\system32\msg.exe"),
Environment.ExpandEnvironmentVariables(#"%windir%\sysnative\msg.exe") };
foreach (string msgPath in msgPaths)
{
if (File.Exists(msgPath))
{
returnedMsgPath = msgPath;
return true;
}
}
return false;
}
And for invoking it :
if (LocateMsgExe(out string strMsgPath))
{
Process.Start(strMsgPath, "* \"Hello world!\"");
}
Regards,
damien.
This is my solution. It consists of a webpage (.aspx) with a listbox (lstComputers), a textbox (txtMessageToSend), a dropdownlist to select the OU that contains the computers that will receive the message and a button (btnSendMessage).
This is the code for btnSendMessage on the aspx.cs
protected void btnSendMessage_Click(object sender, EventArgs e)
{
string searchbase = ddlZaal.SelectedItem.Text; //This is a dropdownlist to select an OU
DirectoryEntry entry = new DirectoryEntry("LDAP://OU=" + searchbase + ",OU=YourOU,OU=YourSubOU," + Variables.Domain + ""); //Variables.Domain is specified in the Web.config
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=computer)");
foreach (SearchResult result in mySearcher.FindAll())
{
DirectoryEntry directoryObject = result.GetDirectoryEntry();
string computernaam = directoryObject.Properties["Name"].Value.ToString();
lstComputers.Items.Add(computernaam); //This is a listbox that shows the computernames. To each computer a message is sent.
string pingnaam = computernaam + "dns.suffix"; //Might be necessary for connecting to the computes in the domain
string MessageToSend = txtMessageToSend.Text; //The text in this textbox will be the messagetext
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(#"C:\inetpub\wwwroot\PsExec.exe"); //Location of PsExec.exe on the webserver that hosts the web-application.
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.CreateNoWindow = true;
psi.Arguments = "/accepteula -s -i \\\\" + pingnaam + " cmd /c msg.exe * " + MessageToSend;
process.StartInfo = psi;
process.Start();
}
}
i'm developing an application to get backup from my postgreSQL database with C#.
i,m using code below to execute and get out put from pg_dump.exe.
ProcessStartInfo startinfo = new ProcessStartInfo
{
FileName = "\"C:\\Program Files (x86)\\pgAdmin III\\1.16\\pg_dump.exe\"",
Arguments = "--host XXX.XXX.XXX.XXX --port 5432 --username \"USERNAME\" --no-password --format plain --verbose --file \"D:\\MYBACKUP.backup\" \"MYDBNAME\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process proc = new Process();
proc.StartInfo = startinfo;
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
var r = proc.StandardOutput.ReadLine();
}
proc.WaitForExit();
Console.WriteLine(proc.ExitCode);
Console.ReadLine();
but proc.StandardOutput.ReadLine always returns null!!!
i try to put pg_dump.exe output to a file with command propt like this:
C:\Program Files (x86)\pgAdmin III\1.16>pg_dump.exe > d:\log.txt
but again log.txt is empty!!
thanks in advance.
Replace RedirectStandardOutput with RedirectStandardError.
My working code is:
ProcessStartInfo startinfo = new ProcessStartInfo
{
FileName = "Path to pg_dump.exe",
Arguments = "Arguments",
UseShellExecute = false,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startinfo))
{
using (StreamReader reader = process.StandardError)
{
StreamWriter sw = new StreamWriter(#"C:\log.txt");
sw.WriteLine(reader.ReadToEnd());
sw.Close();
}
}
I can in C# do the following:
var pSpawn = new Process
{
StartInfo = { WorkingDirectory = #"C:\temp", FileName = fileToRun, CreateNoWindow = true }
};
pSpawn.Start();
and this works fine.... however I am wondering if there is a way to run a command (ie: "dir /b") without having to encapsulate it in a batch file?
Just start the cmd.exe and pass the arguments required
var pSpawn = new Process
{
StartInfo =
{
WorkingDirectory = #"C:\temp",
FileName = "cmd.exe",
Arguments ="/K dir /b" }
};
pSpawn.Start();
I have added the parameter /K to leave the command window open so, it is possible to see the output of the command dir.
Of course I think that you are really interested to catch the output of the command.
In this case you could work with something like this:
StringBuilder sb = new StringBuilder();
var pSpawn = new Process
{
StartInfo =
{
WorkingDirectory = #"C:\temp",
FileName = "cmd.exe",
Arguments ="/c dir /b",
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false
}
};
pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
pSpawn.Start();
pSpawn.BeginOutputReadLine();
pSpawn.WaitForExit();
Console.WriteLine(sb.ToString());
You can call something like this:
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c dir /b";
Process.Start(info);