Win32Exception Access Denied - c#

So im trying to run python code in C#. But when I'm trying to execute the command I get a error message:
System.ComponentModel.Win32Exception: 'Access Denied'
Code:
private void run_cmd(string cmd, int args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C://Users//user//AppData//Local//Microsoft//WindowsApps//python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
The error is happening at:
using (Process process = Process.Start(start))

Process.Start Method is not supported in UWP apps. To run the exe file from your UWP app, I'd suggest you use FullTrustProcessLauncher Class.
First, you need to copy the exe file and save it in the UWP app package, for example, the Assets folder. Then please go to the Manifest file and add the runFullTrust restricted capability.(App capability). Next, add the reference of Windows Desktop Extensions for the UWP project. At last, you could call await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(); to launch the exe file from your app.

Related

Exception "The requested operation requires elevation" when i run my .net code

"I'm trying to execute "main.py" file which is written in Python by my .net core web API but i got an exception.
I already give the permission of my Web API folder as well as my Python Code folder.
var file = Configuration.GetValue<string>("DE.PythonPath");
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = file;
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
string stderr = process.StandardError.ReadToEnd();
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result;
}
}
I expect the python code run but it gives exception "The requested operation requires elevation"
Visual studio need to be run as admin. So run the Visual studio as Run as Administrator

Unable to run python script in c# which runs scrapy spider

I followed this_link and I was able to run a dummy python file from my c# code like this...
public JsonResult FetchscrapyDataUrl(String website)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\ProgramData\Anaconda3\python.exe";
start.Arguments = #"C:\Users\PycharmProjects\scraping_web\scrape_info\main.py";
//this is path to .py file from scrapy project
start.CreateNoWindow = false; // We don't need new window
start.UseShellExecute = false; // Do not use OS shell
//start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
Console.WriteLine("Python Starting");
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
Console.Write(result);
}
}
}
Now I know that I can run scrapy spider from a single file main.py like this...
from scrapy import cmdline
cmdline.execute("scrapy crawl text".split())
When I run main.py file from cmd in windows it works fine but it does not work when I run it from C# code .Net framework. The error is ...
"Scrapy 1.4.0 - no active project\r\n\r\nUnknown command: crawl\r\n\r\nUse \"scrapy\" to see available commands\r\n"
Any Idea how to run this...Or am i missing some path setting in windows ??
Or should I run my spider from C# in some other way??
You need to set the WorkingDirectory property
start.WorkingDirectory = #"C:\Users\PycharmProjects\scraping_web\scrape_info\"
Or you need to cd to that directory to make it work

externally access processing via cmd

As I aksed in another post, I am trying to automate running processing ide from c#. Finally I found the way to run the processing sketch via cmd, with setting the installed processing folder in the path of evironment variable.
I find it works with inputting command directly in cmd.exe, but when I want to do the same thing through some c# code in Visual Studio, it doesn't run the .pde file.
Here is the code,
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Runprocessing
{
static void Main()
{
Process process = new Process();
ProcessStartInfo stinfo = new ProcessStartInfo();
stinfo.FileName = "cmd.exe";
stinfo.Arguments = "/c"+"processing-java --run --sketch=D:\\pw --output=D:\\pw\\output";
stinfo.CreateNoWindow = true;
stinfo.UseShellExecute = false;
process = Process.Start(stinfo);
process.WaitForExit();
process.Close();
process.Dispose();
}
}
}
My question is, how should I properly use processing-java to activate the sketch. because here I am stating
stinfo.FileName = "cmd.exe";
stinfo.Arguments = "/c"+"processing-java --run --sketch=D:\\pw --output=D:\\pw\\output";
Is this the right way to use processing-java in cmd?

Process Start Did Not Work

I have a windows service which is writing in C#.
When I start debug the program it is working without any problem. But if I publish it does not work and I am not getting any error in my log file or eventviwer.
I checked the UAC to never notify.
Also my code is like this:
using (Process tvProcess = new Process())
{
tvProcess.StartInfo.WorkingDirectory = installationPath;
tvProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
tvProcess.StartInfo.UseShellExecute = true;
tvProcess.StartInfo.FileName = TVAppFileName;
bool started = tvProcess.Start();
tvProcess.WaitForInputIdle();
Logger.TraceFormat("TV - TV was started from the path {0}. Status : {1}",
System.IO.Path.Combine(installationPath, TVAppFileName),
started);
}
Also my installationPath = #"C:\Program Files (x86)\TeamViewer\Version8\"
TVAppFileName = "TeamViewer.exe"
How can I start this process without debug?
Thanks

Add to my solution folder with exe file that i want to run

I added to my application folder with exe file that i want to run from my application but i think I did not run the exe file properly.
For example my folder name is folder and the exe file is run.exe so i try #"\folder\run.exe" but The system cannot find the file specified.
what is the correct way to do it ?
public void run(string filePath, int deviceNumber)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(#"\folder\run.exe");
processStartInfo.Arguments = string.Format("{0} {2}{1}{2}", (deviceNumber).ToString(), filePath, "\"");
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
using (Process process = Process.Start(processStartInfo))
{
process.WaitForExit();
}
}
ISSUE SOLVED:
the way to do it is ProcessStartInfo processStartInfo = new ProcessStartInfo(System.Windows.Forms.Application.StartupPath + myEXEpath);
If you run it using Process Class remove the leading backslash
#"folder\run.exe"
The directory "folder" must be in the same directory with your executable.
When you start your app from VS you could start processes programatically like this:
Process.Start(#"C:\somepath\run.exe");
You can set the process path relative to your app path.
Like that:
Process.Start(AppDomain.CurrentDomain.BaseDirectory+"run.exe");
You can run this from program.cs and run it at same time of you application start or start it on an other event like form load, or a button click.

Categories