Permission issues when running JScript from C# Console application - c#

I'm trying to run a Jscript task from a C# console application.
The Jscipt file is not mine so I can't change it. The script moves some files and this is what is causing the issues.
When I run the script manually, i.e. form the shell it executes correctly. When I try and run the script from my console application the bulk of the process runs but I get a ":Error = Permission denied" error when it tries to move the files.
I've tried every permutation of the Diagnostics.Process class that I can think of but I've had no luck.
My current code:
Process process = new Process();
process.StartInfo.WorkingDirectory = Path.GetDirectoryName((string)path);
process.StartInfo.FileName = #"cmd.exe";
process.StartInfo.Arguments = "/C " + (string)path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Verb = "runas";
process.StartInfo.LoadUserProfile = true;
process.StartInfo.Domain = "admin";
process.StartInfo.UserName = #"cardax_sync_test";
process.StartInfo.Password = GetSecureString("abc123");
process.Start();
process.WaitForExit();
Any ideas?
Thanx

Rookie Mistake!
I forgot to close the text reader that creates one of the input files for the jscript.
I'll submit this question for deletion when it get's old enough. Don't want more useless info clogging up the net!

Related

Changing windows theme in C#

I don't know how to explain...Custom windows themes, I can apply them from any of this:
Open the .theme file easily
From CMD : call %windir%\Resources\Themes\Shady.theme
From Powershell : Invoke-Expression "C:\Windows\Resources\Themes\Shady.theme"
but when I use any of this with C# it's breaking the theme - not applying it completely. I tried Verb run as , Tried Making a ps1, bat files and run it with C# but still the same problem... See these screen Shots to understand what I mean
my code:
string tokyo1 = "call " + '\u0022' + #"%windir%\Resources\Themes\Tokyo Night.theme" + '\u0022';
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + tokyo1;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Verb = "runas";
process.Start();
With C#
With cmd,powershell,theme file
I assume you are just using shell execute from cmd.exe so that you don't need to decide what program should open the .theme file. But that could be done more easyly without calling cmd.exe before just do this:
var windir = Environment.GetEnvironmentVariable ("windir");
var filename = Path.Combine (windir, "Resources\\Themes\\Shady.theme");
System.Diagnostics.Process.Start (filename);
Thanks for all who tried to help.
I Fixed it !
Just Used Windows Forms App
Not the old one

.exe file behaves differently when run from Unity to when open manually

I am attempting to run a .exe from Unity. The .exe runs perfectly when I open it manually by double-clicking, but from Unity, it just opens then does not work at all.
The .exe is a very basic python script (that I made into an executable) that reads a text file and then creates another one. When run from Unity the executable window says that this file does not exist/can't be found, when I know it does, and then immediately closes.
I have tried running this .exe with these methods:
Application.OpenURL(path);
And:
Process.Start(path);
The .exe works perfectly fine when I click on it and has no dependencies or anything other than that one text file.
How can I run this file from code as if it had just been clicked?
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = path;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.EnableRaisingEvents = true;
process.Start();
But I think that your path is invalid. Can you Debug.Log it and write in the comment what it is? It should be absolute path btw
The solution is to define the process' directory like this:
Process p = new Process();
p.StartInfo.FileName = path + "app.exe";
p.StartInfo.WorkingDirectory = path;
p.Start();
with path being the path to the folder where the app.exe is located.

How to combine multiple gz files into one from Process in C# program when one is missing EOF

I have multiple .gz files in a directory (2 or more), with at least one file missing the end of file marker. Our C# process is unable to read the file with missing end of file, but since they are coming from a third party we do not have control over how they are created.
As such, we've been running the following Linux command manually:
cat file1.gz file2.gz > newFile.gz
In order to automate this, I am looking for a way to leverage the Process functionality in C# to trigger the same command, but this would only be available in Cygwin or some other Linux shell. In my example, I'm using git bash but it could be Powershell or Cygwin or any other available Linux shell that runs on a Windows box.
The following code does not fail, but it does not work as expected. I am wondering if anyone has recommendations about how to do this or any suggestions on a different approach to consider?
Assume that the working directory is set and initialized successfully, so the files exist where the process is run from.
Process bashProcess = new Process();
bashProcess.StartInfo.FileName = #"..\Programs\Git\git-bash.exe";
bashProcess.StartInfo.UseShellExecute = false;
bashProcess.StartInfo.RedirectStandardInput = true;
bashProcess.StartInfo.RedirectStandardOutput = true;
bashProcess.Start();
bashProcess.StandardInput.WriteLine("cat file1.gz file2.gz > newFile.gz");
bashProcess.StandardInput.WriteLine("exit");
bashProcess.StandardInput.Flush();
.
.
.
bashProcess.WaitForExit();
My expectation is that newFile.gz is created
I was able to find a solution to my problem using a DOS command, and spawning a cmd Process from CSharp.
My code now looks like this, avoids having to launch a linux-based shell from Windows, and the copy command in windows does the same thing as cat:
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments = #"/C pushd \\server\folder && copy *.txt.gz /b
combined.gz";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
System.Threading.Thread.Sleep(2000);
string line = proc.StandardOutput.ReadLine();
while (line != null)
{
output.Append(line);
line = proc.StandardOutput.ReadLine();
}

running a JScript file in cmd.exe by a button

I have a button in my form that when its clicked, it will open command prompt and automatically run a javascript file. My code so far only opens the command prompt. How do you run a javascript file?
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.Start();
Now that we know that you're trying to launch a Node.js app, try this new version.
And don't forget that C# is pretty well documented on MSDN.
If the rest of your code is working as is, and you don't actually want/need to run CMD.exe, this should do the trick:
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\nodejs\node.exe";
process.StartInfo.Arguments = #"P:\ath\To\Your\File.js";
process.Start();
As a holdover from the original question: If you had been trying to launch a JScript script, you would want to use #"C:\Windows\System32\Cscript.exe", or if you want to launch a JScript script without seeing a command prompt window, replace Cscript with Wscript.
Try this:
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("Cscript.exe \"PathToYourFile\\file.js\"");
//OR
//process.StandardInput.WriteLine("Wscript.exe \"PathToYourFile\\file.js\"");
process.StandardInput.Flush();
process.StandardInput.Close();

Process stuck with black cmd screen

So I have this code to launch a bat script which will execute certain java commands, starting with "java -version" just to get some output. The first time I call it it works, but the second time I am stuck with a black cmd screen.
The same code is used but in different locations.
Process proc = new Process();
ProcessStartInfo StartInfo = new ProcessStartInfo();
StartInfo.RedirectStandardOutput = true;
StartInfo.RedirectStandardError = true;
StartInfo.FileName = path + "javaScript.bat";
StartInfo.Arguments = "\"" + path + "\"";
StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = false;
proc.StartInfo = StartInfo;
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
Anyone can help me figure out what happens? Since I don't get any echo I doubt the bat file gets stuck anywhere (echo is on and the first command is java -version so it should write something instead of just getting stuck at black cmd window)
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
You are deadlocking the process with this code. It cannot exit until you empty its output buffer. But you don't read its output until it exits. The program can't continue, nor can you. A "deadly embrace", better known as deadlock.
Simply swap these two lines of code to fix the problem.
Do note that you have a problem with StandardError as well, it will still deadlock when it sends a bunch of error text to that stream. If you don't want to read it then don't redirect it. If you want to make it completely solid then you'll need to use BeginErrorReadLine and BeginOutputReadLine.

Categories

Resources