Run an executable present in Windows Path using C# - c#

I'm trying to run some commands, like rails test, using a C# command line. I tried using How To: Execute command line in C#, get STD OUT results but I'll need full path to the rails executable for that to work. Is there any alternative that will find work just like the windows command line does?

If you can P/Invoke, you could locate the executable with PathFindOnPath. A quick google doesn't show a C# equivalent.
without P/Invoke, Environment.GetEnvironmentVariable("Path").Split(";") should give you a list of paths to probe.
However, this is not the entire resolution used by ShellExecute or even the console.

I believe if you have UseShellExecute set to true in the ProcessStartInfo used to start the process, it'll use the path. Haven't checked it yet - will do so when I get a chance.

Related

Opening a URL in windows explorer without COM Object

The problem
I'm trying to open a URL in internet explorer. Regularly I would use the Navigate method of the Internet Explorer com object. However that isn't available in the programming language I am using (MapBasic).
My next method would be to use the Shell function to execute a command in the command line. For example:
explorer "http://yahoo.com"
Will navigate to http://www.yahoo.com. However when it comes to longer links, e.g.
https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8
Command prompt actually navigates to:
https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbn
Which is a significantly shorter URL.
To solve this issue I am looking into using the Win32API, but I can't for the life of me figure out how I could run this code with Win32API... Perhaps using Shell32.dll's ShellExecute? Or would it be better to use Kernel32.dll's CreateProcess?
Whichever is used, it would be really helpful if someone could supply me with some example code of how to open the above link with IE using the Win32 APIs in VB/C#. I can easily port the code to MapBasic from there!
(Alternatively if there is a method to open the link through shell, I would love to know how!)
Thanks
You can just "execute" it using Process.Start which is under System.Diagnostics name space.
As Hans Passant replied:
It is 259 characters long. Magic number. Don't use Explorer.exe, use a
browser like iexplore.exe
This is exactly the solution I needed. Using iexplorer.exe instead:
"C:\Program Files\Internet Explorer\iexplore.exe" "https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8"
Opens the URL fine.
Similarly one can also use the following command:
start iexplore "https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8"
Which should be less system dependant.

Universal Windows Platform (UWP) C# app - How to run stand alone python inside your application

I am trying to find a way to call python from my UWP app. So far I have a .exe file that I have compiled from python using pyinstaller (www.pyinstaller.org/). This basically allows me to package up my python script as a standalone binary (ie: you don't need python to run it). This all works well and I can call my wrapped up python .exe via cmd.exe no problem:
$ process.exe -p "path\to\file"
$ Processing file: "path\to\file"...
$ Done.
So now I just need to call it from my UWP app - so I have added it to my application like so:
C# Project
Assets/process.exe
Frustratingly, I've not had much luck googling for answers to my problem - my attempted solutions so far have included:
Calling the "Assets/process.exe" directly from my app
Looked at "Launch an app and get results". I think this seams to be for external applications however... I certainly didn't get it going anyway.
Opening the cmd.exe (somehow) and calling my process.exe from there.
I'm not even sure if I'm trying to do this the correct way or not. Or if I have just not understood some of my findings. Or (fingers crossed) there is a simple solution to this I just don't know about and have somehow missed as I'm very new to UWP development and C#.
So any solutions/pointers here would be greatly appreciated thanks!!
UWP apps are 'sandboxed'; i.e. they have many security restrictions placed upon them to isolate them from the rest of Windows (like not being able to read/write to the Registry and not being able to directly access random files from the file system).
So there is no way to run an .exe (or any other executable) from your UWP app. If you have access to a StorageFile (say music, video or any other file format) then you can launch the file in the default program associated with that file type.

Is it possible to run .scf file programmatically

Am trying to run "show Desktop.scf" using System.diagnostics.Process.Start() in C#. But the debug line just passes and takes no action.
When I try using Start -> Run, it performs the show desktop action.
Process.Start takes a filename. Why wouldn't this work from the code. I tried in Admin mode too.
Any guidance pls.
Hand in a ProcessStartInfo with UseShellExecute set to true.
This executes the scf in the same way START -> RUN would do.
shell:::{3080F90D-D7AD-11D9-BD98-0000947B0257}
in Start -> Run does the same as running the .scf
I was making an AutoHotKey script when i encountered the same problem.
But when i replaced the path to .scf with the above code it worked like a charm.

Output the result of a process

I wanted to make an online c# compiler and also run the compiled program and output it to my website.
How can I do this? I have understood the implementation of the c# compiler but have not been able to display the output of the compiled program on to my website. The process.Start function will just initiate the .exe but how do I display the results from that .exe file?
You need to start the process using a ProcessStartInfo object specifying RedirectStandardOutput to true.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
You need to redirect standard ouput of your process.
Please note that you need to make sure users aren't running arbitrary code on your server. How do you protect against C# code that tries to format your disc?

How to launch a JVM from C#

I would like to launch a java application from C# but am unsure of the proper way to do it. I can do it from a bat file:
java -cp ".;other_classes_location" classname
How does this get turned into C#?
What you have posted is a command line (likely executed in a command shell)
you cannot turn this into C# code
I guess you are looking for a way to execute that command using C#. In this case the answer is the below code
System.Diagnostics.Process.Start("yourPath\java.exe", "Command Line Arguments");
Use Process.Start.
Examples here.

Categories