Install util looks for service on wrong folder - c#

3
I'm trying to install a Windows Service using a batch file, let's call it "setup.bat". Inside the file I have the following commands:
"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil" "MyService.exe"
When I excute the batch file (running as administrator on windows7) I get this:
Exception occurred while initializing the installation:
System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Win
dows\system32\MyService.exe' or one of its dependencies. The system cannot f
ind the file specified..
The actual service is located at
"SomeRandomLocation\MyService.exe".
the bat file is
"SomeRandomLocation\setup.bat"
what is going on? how do I force it to install from my "setup.bat" folder?
this should work dynamicly. meaning in any folder!

I don't know anything about the install process. But %~dp0 will give the absolute path of your running batch file. So if your exe is in the same folder, you could try:
"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil" "%~dp0MyService.exe"

If you create a .bat file, then the working directory is based on the location from where you invoked the .bat. If you created a shortcut to the .bat file, then the working directory is based on the location of the .bat file. Any relative path in your script is interpreted relative to the working directory.
To avoid changing all your paths. Just issue a cd C:\Services at the beginning of your bat file.

For my own usage I created a small .bat file:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" %1
And now, when I want to install service I just drag .exe file on the .bat. Works perfect :)

Have you tried setting working directory to C:\Services?

although this is an old question...
what helped me to solve the issue is to run the installer with logs
For example:
"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil" "MyService.exe" /LogToConsole /ShowCallStack
In my case the original error was like yours
FileNotFoundException
, but in the logs I found
SecurityException
The solution was to run as administrator

It works for me (of course) - It looks for MyService.exe in the containing folder.
"c:\windows\system32" is not the location of installutil, so perhaps earlier in the batch file the working directory is changed somehow.
Presuming this is not the only command in the batch file:
Try to add set OLDDIR=%CD% at the very beginning of the batch file,
And add chdir /d %OLDDIR% just before the installutil command, and see if this works.

Related

C# File Disappeared after FileInfo.MoveTo with Local Path (Windows)

I Ran FileInfo.MoveTo("filename.txt") with just a name instead of passing a full path and the file just disappeared. I believe in linux this would make it go to the root directory "/", but on Windows I'm not sure if there is a such thing as a root directory beyond just C: Is there any way to locate my lost file?
It is likely in the working folder that your executable is running from. For example, MyApp\bin\Debug, depending on the configuration you are running in.
It should be in project folder. Usually files without specefying path are saved there. (in folder with .exe file)

why to I get this error: System.UnauthorizedAccessException: 'C:\Windows\system32\CSV_file'

I have written a code in C#.
The code is loging into a file the following:
CPU Usage
RAM Usage
Network card traffic
Current time
I want that on system startup the code will be executed automatically.
So I have written a batch file that runs the .exe file like that:
START 'C:\Debug\DiagnisticTool.exe'
while DiagnisticTool.exe is the application and it is contained in a folder named 'Debug' at C drive.
The batch file is in the windows startup folder.
While I start my PC the batch file is running and than the error stated above comes up.
This is how the file is been written in the script code:
using (StreamWriter outputFile = File.AppendText("PerformanceLogFile.csv"))
{
outputFile.WriteLine(something to write to file);
}
yet the error stateds that it tries to write it somewere else: 'C:\Windows\system32'.
Why is that?
When I run the program via visual studio or via the .exe application that it created it is runing OK.
Plus when I run the batch file manually it works fine.
The ONLY problem is that it doesn't run at startup as it should.
This happens because when your program is called by the batch the current folder is not the one you have your executable and windows doesn't care to change the current directory to your folder. The only thing that matters is the availability of your program and this is granted because you give the full path to the START command.
You can fix your problem changing the START command and specifying the folder where your program should be run
START /D C:\Debug C:\Debug\DiagnisticTool.exe
or adding a CD command just before running the file
CD C:\DEBUG
START C:\DEBUG\DiagnosticTool.exe
or changing the path passed to StreamWriter
using (StreamWriter outputFile = File.AppendText(#"C:\debug\PerformanceLogFile.csv"))
this last option, while allowing to leave your batch as is, could be a problem if you want to customize the output folder. For example if you are not allowed to create a C:\DEBUG folder on the target machine or for some other reason.

batch works fine but "windows cannot find devcon.exe" in c# program

I have a batch file which disables and enables some driver using windows devcon.
when I run this batch file by double-clicking on it it works fine.
However, I tried to run it from a C# program that I wrote using this line:
System.Diagnostics.Process.Start("C:/*path to file*/file.bat");
it runs fine until it tries to open the devcon.exe and I get the following message:
after that it continues to run smoothly.
any ideas why it doesn't work from the C# program?
p.s
I can't post the batch code due to IP issues...
The problem is - as often - the "working directory". When you double-click something in the Explorer, the working directory is set to the current folder, so from the batch file's point of view it's current directory is its own directory.
When you execute a C# application, usually the working directory is the directory of the application's exe file, but not necessarily (for example if the application is run using a link, you can specify a different working directory). That's why, to find the application EXE file's directory it is not save to use GetCurrentDirectory.
So what happens is that the application runs the batch file, but with the application's directory, not the batch file's directory, as working directory. An alternative to an explicit cd within the batch file would be to specify the working directory when calling Process.Start.
ok, after a little bit of research I found this simple solution:
simply changing to the directory of the devcon.exe (using cd command) at the beginning of the batch code, i.e:
cd "C:/*path to the directory of devcon.exe*"
#rest of the code

XML file couldn't be detected after windows service setup

I have created a windows service. I do the setup of the service using the windows installer.
I have one XML file like a config file. Whenever i debug windows service with attaching the debug project it works fine. But after the installation i can see the event saying couldn't find the example.xml file in C:\Windows\system32. It's a repeating problem. Can anyone tell me whats going on with this. Or any suggestions?
This isn't an installer problem. Windows Services are always started with System32 as the current directory. Add this line to the Main() method in your Program.cs prior to firing up any services.
Environment.CurrentDirectory = new FileInfo(Assembly.GetExecutingAssembly().FullName).DirectoryName;
Most likely file is just not where you are looking for it.
Chances are that your code looks in "current working folder" which during debugging in VS is the same as application, but in case of starting as service/from command line is different.
Make sure your code loads file from location you expect the file to be (i.e. next to the application), but not from some relative path (like "myfile.xml").
Why are you saving stuff in the Windows folders? Unless there's a really good reason to do so, this is bad. Install your config in the same place that your app is running and get the file path with:
var folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var configPath = Path.Combine(folderPath, configFileName);
The issue is that your service is not running as Administrator. If the service is not running as Administrator, then it won't be able to read the files under system32 folder.
Change the service properties to run as Administrator. That should fix the issue.

Run program from windows service

I have a program that is stored in program files folder.
I created windows service to run it, but when I do, it doesn't start.
I used process monitor to see what happens, and realized that it's looking for all dependencies in system32 folder.
If I take the program, with all referenced dependencies and paste them into the root of system32 folder, it works! But I don't like it that way, I want to run it from specific folder.
Add your program's folder to PATH environment variable and your service will find it there...
The PATH trick didn't work. It gave me error about some depended file software were trying to locate. But I found the solution. You can provide working directory:
process.StartInfo.WorkingDirectory
That did the trick. Thanks anyway!

Categories

Resources