Starting program in C# including all files - c#

I'm having problems running a process from my program.
When I start the process it says "Cannot find Tibia.dat!" ( it thinks the exe file is located in project directory, when it isn't ).
So when I start the process in my program ( from: C:\program\Tibia\Tibia.exe ) it says "Cannot find C:\user\marcus\my documents\visual studio 2009\blablalba\Tibia.dat".
Here's the code I'm using:
Process.Start(addressToFirstTibia + "\\Tibia.exe");
Grateful for help !!

You need to set the working directory. Tibia.exe probably expects it to be the same as the executable's directory, so try:
Process.Start(new ProcessStartInfo {
FileName = Path.Combine(addressToFirstTibia, "Tibia.exe"),
WorkingDirectory = addressToFirstTibia
});

Is Tibia.exe looking for Tibia.dat internally? It may be detecting somehow that the "current working directory" is the project directory, not its own executable directory.
There's a property called WorkingDirectory on ProcessStartInfo that may solve this issue for you. Info can be found here.

Related

Why does C# Process.Start not find the executable on my path?

I have this folder on my windows Path environment variable: C:\bin
I have a tool named 7z inside C:\bin\7z\7z.exe.
If I open any shell, like Powershell I can run the command "7z" and it works fine, the executable is found and it runs (And where.exe 7z prints out C:\bin\7z\7z.exe). The fact that the executable is inside a folder named after itself doesn't get in the way of the executable being found. The folder and the executable inside the folder seem to have to be named exactly as the program I am trying to run.
However, when running code in C# to run the executable, it is not found.
ProcessStartInfo startInfo = new ProcessStartInfo() {
UseShellExecute = false,
CreateNoWindow = true,
FileName = "7z",
Arguments = $"-h",
};
var p = Process.Start(startInfo); // Throws
I know that Process.Start supports Path environment variables here as I can successfully run this code for other executables that are not in inner folders.
My questions are:
Why isn't Process.Start able to find the executable?
Is having a directory named after the executable a supported and documented way to find executables with a Path directory? Where can I read this documentation?
If you set ProcessStartInfo.UseShellExecute to false, you must supply the full path to your executable. In this case Process.Start will call the CreateProcess function. From the documentation:
The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.
If you use ShellExecute true, the shell's best guest for 7z will be the directory, as you dont supply the .exe extension.

C# calling batch file failed to copy dll files to System32 folder

I have created a batch file that copies some dll files into System32 folder.
I ran this batch from my program written in C# code:
string path = #"ABC\install.bat";
ProcessStartInfo procStartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
CreateNoWindow = false,
FileName = "\"" + path + "\"",
//Arguments = "\"" + path + "\""
Verb = "runas"
};
using (Process proc = new Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
}
Everything has worked fine. I got the popup message to confirm the change from Windows 7. The console also proved that the file had been copied:
C:\Program Files\XXX>copy commpro.dll C:\Windows\system32\
1 file(s) copied.
But when I look in System32 folder I couldn't find my dlls there.
It's so strange!
Does anybody occur this issue?
Edit: My question is different with this question: How to write files in C:\Windows\System32 with full permissions
Here I got the popup that allow me to grant permission to write to System32 folder. And the output of "copy" command didn't show "Access Denied" but "Copied".
The problem is why it doesn't contain my dlls while it said "copied" ?
If your app is a 32-bit application, then the file will end up in the %windir%\SysWOW64 folder. See this page on Msdn for more details.
Your 32-bit application should be able to see this file.
I should point out that copying dlls to your system folder is usually a bad idea and should be avoided if at all possible.

Trying to run slui.exe from within a method using ProcessStartInfo and Process

I'm having an issue with running slui.exe from a method in c#. I'm using the code:
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Windows\System32\slui.exe");
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
but I keep getting a Win32Exception: 'The system cannot find the file specified'.
If I change the ProcessStartInfo to: (#"C:\Windows\System32\cmd.exe") it will launch just fine.
Is there something with running slui.exe in this context that is breaking?
I'm certain that the file is in the directory specified, so I'm stumped as to what may be going wrong here.
Any ideas how to call slui.exe from a c# method?
Slui.exe is only available as a 64-bit program on Windows x64. Your hard-coded path c:\windows\system32 will get re-directed to c:\windows\syswow64 when you run as a 32-bit process. And thus won't find the file.
Project + Properties, Compile tab, change the Platform target setting to "AnyCPU". Repeat for the Release configuration. And use Environment.GetFolderPath() to ensure it still works when Windows isn't installed to c:\windows.

C# run batch file, command not recognised

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!

Running Latex from C#

When running latex from C# using Process.Start, I'm getting this error: "latex: A required file system path could not be retrieved." It runs fine from the command line, so I'm not sure why it doesn't run from Process.Start. Has anyone run into this issue?
Edit: Also, this is from ASP.NET!
Thanks!
Without seeing more code, my best guess would be to set the WorkingDirectory of your StartInfo class to whatever directory it works from on the command line.
ProcessStartInfo startInfo = new ProcessStartInfo(#"\path\to\latex\latex.exe");
startInfo.WorkingDirectory = #"\path\to\latex";
I've run into this problem before with other EXE's and that seemed to be the fix.
The issue was IIS permissions.

Categories