I have created an Utility.exe using C# which allows users to choose one of the multiple versions of an application. The user runs the Utility.exe and clocks on one of the version buttons in the Utility window which triggers a batch file for that version and then the respective application.exe.
The problem I am facing is after the Application.exe is triggered. The user logs into the application but gets the error Database Handle is Invalid.
But, when the user goes to the Application version folder and runs the batch file and then launches the application, it works fine.
My assumption is that when we trigger the batch file through the utility.exe, it does not discern the file path for the OCX and DLL files and thus does not register them properly.
A workaround is to specify the file path for the OCX and DLL files in each of the version batch files on each of the user's desktops.
Is there a way I can specify the file path in the utility code (C#) for the OCX and the DLL files mentioned in the batch file?
EDIT: I tried using process.StartInfo.WorkingDirectory as suggested by Dave but still getting the same issue.
private void button1_Click(object sender, EventArgs e)
{
string batDir = string.Concat(#"C:\Program Files (x86)\Blah\BlahBlah\batch.bat");
Process proc1 = new Process();
proc1.StartInfo.FileName = batDir;
proc1.StartInfo.WorkingDirectory = #"C:\Program Files (x86)\Blah\BlahBlah";
proc1.StartInfo.CreateNoWindow = false;
proc1.StartInfo.Verb = "runas";
proc1.Start();
proc1.WaitForExit();
string exeDir = string.Concat(#"C:\Program Files (x86)\Blah\BlahBlah\application.exe");
Process proc2 = new Process();
proc2.StartInfo.FileName = exeDir;
proc2.StartInfo.CreateNoWindow = false;
proc2.Start();
}
Hopefully, I have been able to articulate my problem.
Related
In my C# WPF application I want to install an other program. The other program consists of a setup.exe, multiple msi files and a vcredist.exe. I need to start the setup.exe because it hands over some parameters and information to the msi files and uses an update functionality for the existing version of the program. So I can't start the msi files directly.
programPath = programPath + #"\setup.exe";
Process programsetup = Process.Start(programPath);
programsetup.WaitForExit();
Files are stored in root directory of my C# app. My problem is that I can't move the files to an subfolder because the msi files are always searched in the root directory and not in the subfolder.
now:
..\myApp\setup.exe
..\myApp\client.msi
..\myApp\host.msi
..\myApp\manager.msi
..\myApp\vcredist.exe
My question: How can I move setup.exe and msi files in a subfolder and start it from there?
What I want:
..\myApp\toolkit\setup.exe
..\myApp\toolkit\client.msi
..\myApp\toolkit\host.msi
..\myApp\toolkit\manager.msi
..\myApp\toolkit\vcredist.exe
Error I get during setup when I do it this way: ..\myApp\client.msi not found.
This code will directly launch setup.exe.
Properties -> right click Open to Resources.resx -> Top Left Add Existing File -> select the file.
byte[] resourceFile = Properties.Resources.setup;
string destination = Path.Combine(Path.GetTempPath(), "setup.exe");
System.IO.File.WriteAllBytes(destination, resourceFile);
Process.Start(destination);
I've solved it with ProcessStartInfo.WorkingDirectory.
Problem/Solution: If you start a setup file with Process which loads during installation some msi files from same directory you need to set WorkingDirectory.
Code example:
string ToolkitExe = myAppPath + #"\myApp\toolkit\setup.exe";
string ToolkitPath = myAppPath + #"\myApp\toolkit\";
ProcessStartInfo startInfo = new ProcessStartInfo(myAppPath + #"\myApp\toolkit\");
startInfo.WorkingDirectory = myAppPath;
Process p = Process.Start(startInfo);
p.WaitForExit();
RunAll();
I am developing a C# windows form application project. The form contains a button and after the button click event cmd.exe is started using Process.Start() method. The StartInfo.Arguments() contains path to an exe file stored in the project resources folder (I have created a new folder called Resources and another subfolder called SW1. The exe file is inside SW1 folder)
Right now, the exe file is stored in a different location in local computer and I use the full path to refernce the exe file in StartInfo.Argument(). However, when i publish the C# application, I want the exe file will be bundled as a resource. How can I reference the exe file in this case?
private async void button1_Click(object sender, EventArgs e)
{
await RunCMDAsync();
}
private async static Task RunCMDAsync()
{
try
{
using (Process myProcess = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/C cd <path to the exe file on my local computer here> & <name of exe --additional arguments>";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
myProcess.StartInfo = startInfo;
myProcess.Start();
myProcess.WaitForExit();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
In the above code, Whenever the cmd.exe process is started, I want to pass two arguments using StartInfo.Arguments()
startInfo.Arguments = #"/C cd <path to the exe file on my local computer here> & <name of exe --additional arguments>";
The first argument is to cd into the folder which contains the exe file
(/C cd <path to the exe file on my local computer here>)
The second argument runs the exe file with some parameters (<name of exe --additional arguments>)
How can I reference the exe file in Resources > SW1 folder after I publish this as a standalone application? Do I need to extract the exe file to a location on the target computer during installing the C# application and use the same code as above or is there a way to directly reference resources folder?
If what you need is to find the directory where your program is running (I'm not sure I understood your problem) then use System.AppDomain.CurrentDomain.BaseDirectory.
string exePath = System.AppDomain.CurrentDomain.BaseDirectory +
"Resources\\SW1\\" + exefilenameWithExtension;
I am creating a winforms project which checks files in folder. It only works when new file put in the folder. I am using FileSystemWatcher. It works fine on D drive but fails on C drive.
I gave EVERYONE full privilige on that folder
I tried publishing it with click once for full trust application. But it failed also with published edition
Tried to run exe file and visual studio as administrator. Nothing changed
Tried absolute path and Extra filters.
It does not raise any errors. Simply does nothing.
Non Working Code
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
teklifwatcher.Path = desktop+"\\XMLTeklif";
teklifwatcher.NotifyFilter = NotifyFilters.LastWrite;
teklifwatcher.Filter = "*.xml";
teklifwatcher.Changed += new FileSystemEventHandler(TeklifXML);
teklifwatcher.EnableRaisingEvents = true;
private void TeklifXML(object sender, FileSystemEventArgs e)
{
//dostuff
}
I solved this problem on my own. I assume filesystemwatcher can't watch c: drive files directly. Because of the security reasons.
But we can use Program Files (X86) folders just like any other application.
Anyone who have similar problems just use filesystemwatcher on a folder at program filex(x86). And give permissions to that folder. Voila! It works
I have a .exe file which checks the system architecture and based on the system architecture it calls the corresponding msi file.
I am trying to run this exe file from C# using the code below
Process process = new Process();
process.StartInfo.FileName = "my.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.WorkingDirectory = "path//to//exe//directory";
Console.WriteLine(process.StartInfo.WorkingDirectory);
process.Start();
process.WaitForExit();
The exe is getting invoked. i can see the application logs and there are no errors in the logs.
But the msi is not getting started or installed.
When I try to run the same exe file manually the msi is installed.
Note: There are other dependency files for this my.exe which are placed in the same directory.
Why am i not able to install the msi from C# program while i am able to do this manually.
I am running the VisualStudios in administrator mode.
You need to execute .exe (and msi) as an administrator.
To ensure that, use:
process.StartInfo.Verb = "runas"
Also, try it removing quiet arguments to see any possible errors.
Is "my.exe" installing your MSI if you call it, isn't it?
I got this resolved after i added Thread.Sleep(). before "process.WaitForExit()"
I have implemeneted a method into my c# program to run a batch file, which runs a virus scan on any files uploaded;
public static Int32 ExecuteCommand(String filePath, Int32 Timeout){
Int32 ExitCode;
ProcessStartInfo ProcessInfo = new ProcessStartInfo();
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
ProcessInfo.FileName = filePath;
Process proc = Process.Start(ProcessInfo);
proc.WaitForExit(Timeout);
ExitCode = proc.ExitCode;
proc.Close();
return ExitCode;
}
Ok now my batch file;
#ECHO OFF
c:
cd "..\AVG\AVG9\"
avgscana.exe /SCAN="..\learninglounge.com.solar.quarantine\" /REPORT="..\learninglounge.com.solar.antivirus\virusReports\report.txt"
EDIT : I do have fully qualified link to avg exe and directories but have replaced here with .. for purposes of posting to stackoverflow. Sorry if this caused confusion.
So my problem is the reporting side of my batch file. I can double click the batch file and it scans and creates the report no problem. When i run it through my c# i get an exit code of 2; command not recognised. It's fine if i remove the report part of my batch file. Now obviously this points to write permissions but I have checked that and the impersonated user has write access on the directory. Is there anything Im missing?
Thanks all
The error states that avgscana.exe isn't located in directory which is set as "current" when you execute command. When you click on your bat file in Windows Explorer current directory is set to directory where bat file is located. Probably your avgscana.exe is located in the same folder so it works fine.
When you execute the command from .Net application current directory remains the same (if you haven't changed it then it will be a folder where .Net app is located). If your .Net app is located not in the same folder as bat file then you will get an error which you're actually getting. You should either specify a full path in your bat file or set Environment.CurrentDirectory in .Net app before launching bat.
HAve You checked that all enviromental variables are set exactly the same as You run the batch file from cmd line. I am pretty sure that there might be some differences. Also what kind of operating system is it. If this requires elevation (vista windows 7) You might actually need to impersonate appropriate user in code.
luke
Ok well ive done a work around, id rather have saved the report there and then but instead i just catch the standard output from the process and then save to a file + any processing.
Weird one this.
In your batch file, you have:
c:
cd "..\AVG\AVG9\"
avgscana.exe
/SCAN="..\learninglounge.com.solar.quarantine\"
/REPORT="..\learninglounge.com.solar.antivirus\virusReports\report.txt"
When the command is executed, the 'current' directory on drive C: is ambiguous. You just move up the directory tree. Same with the avgscana.exe command, you make relative references instead of absolute.
Are you certain that the CD command has brought you to the correct directory? Change the CD command to an absolute reference ( cd "c:\wherever\avg\avg9\" ), and that way you can be sure your avgscana command's relative references are found.
Your CD command may be failing silently but your avgscana.exe may be in your path, so it does get executed, but since the relative /scan and /report locations are not found, it fails with a code 2.
Good luck!