Hit url against string but not responding - c#

I created the service called SMS serivice where i want to hit the url through the C# program
i use the following code for the same when i attach the service to the program and debug through it, I found that the code what i used is not hiting the borwser
My program :-
using System.Diagnostics;
string proc;
strUrlPath = "http://devices.panhealth.com/DeviceReading.aspx?Source=" + strSMS ";
proc= Process.Start("IExplore.exe",strUrlPath);
Still url is not hit.
but when this url copy past into IE it is working fine i want to hit it through program as following.

You can use the System.Net.WebRequest class instead.
WebRequest request = WebRequest.Create("http://devices.panhealth.com/DeviceReading.aspx?Source=" + strSMS);

just wonder...
string proc;
but
Process.Start
outputs a Process variable type.
from Object Browser info:
public static System.Diagnostics.Process Start(string fileName, string arguments)
Member of System.Diagnostics.Process
Summary: Starts a process resource by
specifying the name of an application
and a set of command-line arguments,
and associates the resource with a new
System.Diagnostics.Process component.
Parameters: fileName: The name of an
application file to run in the
process. arguments: Command-line
arguments to pass when starting the
process.
Returns: A new
System.Diagnostics.Process component
that is associated with the process,
or null, if no process resource is
started (for example, if an existing
process is reused).
and, unless you have changed your %PATH% system variable to include the IE path, you need to use the full path
Process p;
p = Process.Start(#"C:\Program Files (x86)\Internet Explorer\iexplore.exe", "http://www.google.com/");

I agree with Max - use either the WebRequest class. This has many advantages, not least because you can check the HTTP status code returned by the request to find out whether the request was successful.

Related

C# ProcessStartInfo: Second process receives no arguments

I'm trying to make a call from one process to start another, supplying starting arguments as separate parameters in a ProcessStartInfo. The starting call uses a URL registered in Windows to find the second program, entered into FileName. I then add a string containing 4 parameters to Arguments. As I have understood it, the spaces in the string indicate the separation of the arguments.
Program 1
//Create starting information
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo() {
FileName = "urlHandle:",
Arguments = "/argA valueA /argB valueB"
};
//Start second program
System.Diagnostics.Process.Start(startInfo);
Program 2
//Print received arguments to a file
Environment.GetCommandLineArgs().ToList().ForEach(a => writer.WriteLine(a + ",\t"));
The second program starts as intended (meaning the URL is working), but the output is incomplete.
[path to .exe of second program],
urlHandle:,
It contains the path to the program, the string set as FileName , but everything put into Arguments is missing.
Does anybody have any ideas why the arguments disappear?
Note 1: If I would add the arguments into the FileName, I would receive them as one string. In order to trigger the behaviour I want in the second program, I must supply it with several parameters instead of one. I know this is possible from testing it manually from the terminal.
Note 2: I'm using .Net Framework, so trying ArgumentList from .Net Core is not an option.
After some more tests I have found the issue. The error does not lie in how I set up and use ProcessStartInfo, but rather in that I am using a custom URL protocol.
In the Windows registry one defines the number of parameters that can be sent via the URL call ("C:\[path to my .exe]" "%1"). The first argument is the Filename, and is as such the only thing sent via the protocol. In order to send more, one is required to add "%2", "%3", etc.
Note: The path itself becomes argument 0 in the receiving program, while the actual sent parameters start at argument 1 and beyond.

Open and Receive values from a bat to exe with C#

I'm trying to create a C# console app to do some proccess. I want to open my demo.exe and send some parameters from a .BAT file to that cosole.
I know the .bat should be something like:
demo.exe -a cclock -cc 1306 -mc 1750
But, I don't have any idea to make my .exe to get the parameters I'm sending.
This is where the arguments to Main method helps.
In an standard C# program entry method is like,
static int Main(string[] args)
Here args[] is the array of arguments passed to your executable via command line.
So in your example,
demo.exe -a cclock -cc 1306 -mc 1750
args is a string array containing following,
{"-a", "cclock", "-cc", "1306", "-mc", "1750"}
You can retrieve these value in this manner,
args[0] = "-a"
args[1] = "cclock"
args[2]= "-cc" ...... and so on
You can use these value for the rest of your code.
Remember that whatever values you pass are broken into separate string values on each occurrence of white-space. Also whatever value you pass will be taken as string. So you have to do your own validation and parsing.
Your application's Main method (typically in Program.cs) can accept a parameter string[] args, which you can access to get the command line parameters used to launch your app. Alternatively, you can also use Environment.GetCommandLineArgs() anywhere in the application to do the same thing.

Build visual studio solution using msbuild from c# code

I want to build my solution file from other c# code using msbuid I have tried
var msbuild_path = #"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
var solution_path = #"D:\Sumit\WorkingCopy\Final\Final.sln";
Process.Start(msbuild_path + " " + solution_path);
but this one throws an error Please help me out!!
According to https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx , the Process.Start method takes two arguments:
public static Process Start(string fileName, string arguments)
So you should change your code to
Process.Start(msbuild_path, solution_path);
What you were doing before was actually trying to run a file named "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe(space)D:\Sumit\WorkingCopy\Final\Final.sln", but no such file exists with that name. The msbuild.exe may exist, but "msbuild.exe D:\Sumit...\Final.sln" is not the filename you meant to pass as the command filename. Also, the argument string was empty, so the system assumed you did not want to pass any arguments to "msbuild.exe D:\Sumit...\Final.sln". But the error message was because the two filenames were mashed into one filename.
Windows allows filenames to contain embedded spaces, which frequently causes problems in dealing with command-line arguments.

c# detect an open web browser

I am writing a program that searches certain web pages before closing. I would like my program to open a NEW WINDOW using the DEFAULT BROWSER. I can have my program focus the newest window instance, and then it will close that instance once it is done.
I have been staring at WebBrowser.Navigate and System.Diagnostics.Process.Start(target) all day but I cant find the sweet spot with either of them.
WebBrowser.Navigate always opens IE, I have been looking in the API and can't find a way to change the program used. Does anyone else see something that I dont? Is there a way to change the application used?
System.Diagnostics.Process.Start(target) opens in a new tab, not a new window like navigate does. However none of the overloaded functions from the API have a way of saying "create a new instance or window".
this is my issue, they both have pieces that I want, but I cant figure out how to get the pieces I need for either one.
I would be extremely grateful for you help. I have been looking for hours now and I can seem to come to a solution.
code sample for Jester:
Process defaultbrowser = new Process();
defaultbrowser.StartInfo.CreateNoWindow = true;
defaultbrowser = Process.Start(target);
int waitTime = Convert.ToInt32(numericUpDown2.Value);
System.Threading.Thread.Sleep(waitTime*1000);
defaultbrowser.CloseMainWindow();
defaultbrowser.Close();
furthermore my Close() function is causing a runtime error that says;
System.NullReferenceException: Object reference not set to an instance
of an object.
which seems silly because too me the above code makes me think that my defaultbrowser is an instance of a process, which is then supposed to be able to call the non-static function "close()".
ok If I got your problem right you are looking for a way to open a web page in the "default" browser.
That can be done by simply make a new process like:
Process.Start("http://google.com");
If you would like to control witch browser gets used you can do it by passing the web address to the browser's exe file as a parameter:
System.Diagnostics.Process.Start("PATH to exe", "Command Line Arguments");
To start the process in the new window pass a ProcessInfo object to the Process.Start
And set the CreateNoWindow
more info on that
Hey To check if it's loaded wherever, do:
if(browser.ReadyState == WebBrowserReadyState.Complete) {
// It's Open!
}
You should use System.Diagnostics.Process like that:
Process Chrome = new Process(); //Create the process
Chrome.StartInfo.FileName = #"C:\Program Files\Google\Chrome\Application\chrome.exe"; // Needs to be full path
Chrome.StartInfo.Arguments = ""; // If you have any arguments
Chrome.Start();

See command line arguments being passed to a program

You may skip this part
I am using a batch file that I have in my thumb drive in order to
mount a true crypt volume. I created that batch file with the help of
this link. on that batch file I have the username and password
that I pass as arguments to trueCrypt.exe in order for it to be
mounted.
Anyways so my question is: will it be possible to see the arguments being passed to a program from a third party process? In other words, will it be possible to see the arguments being passed to this program:
using System;
using System.Reflection;
using System.Diagnostics;
class Program
{
static string password = "";
static void Main(string[] args)
{
if (args.Length > 0)
password = args[0];
// get location where this program resides
var locationOfThisExe = Assembly.GetExecutingAssembly().Location;
Console.Write("Press enter to start a new instance of this program.");
Console.Read();
var randomArgument = new Random().NextDouble().ToString();
Process.Start(locationOfThisExe, randomArgument);
// I am passing a random argument to a new process!
// is it possible to see these arguments from another process?
}
}
Edit
I am creating an edit cause I think I explained my self incorrectly but this edit should be a solution instead of a question
I think this question has not received enough attention. Executing the command showed by https://stackoverflow.com/users/235660/alois-kraus shows:
(I pasted the output on notepad++)
on the image it does not show very clearly but I was able to see the argument being pass to that process. That matters a lot to me because I mount my true crypt volumes with the command:
"C:\Program Files\TrueCrypt\TrueCrypt.exe" /v "a:\volume.tc" /lz /a /p a
that tells to truecrypt that I want to mount the volume located at a:\volume.tc on drive letter z and the password is a
If I execute that command true crypt will mount that volume on drive z:
the problem is that If I then execute the command wmic process note what shoes up:
Note the password is in there!
So in summary it is not safe to pass secure information as an argument. It may be secure if you close the process that received the arguments but I think it is important to be aware of this...
If other users with administrative rights or with the same user account can execute programs you can see all command lines with
wmic process
from all processes with this single command line.

Categories

Resources