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!
Related
Is it possible to attach a flat file to windows-service?
My windows-service uses few flat file (for data purposes). Usually, if it's a normal executable I would place those files in the same directory as exe. How can I achieve that with windows-service?
I've done some research on this, but all I found was:
1. Pass a path to those files as a parameter to windows-service.
2. Make a fixed path and just require those files there
But I don't like those solutions. I was wondering if it's possible to attach those files to the windows service while installing it?
How about adding these files inside the project as Embedded Resources? They won't show up on the disk, but you could still properly read them from inside the assembly itself.
Here's some reference: https://support.microsoft.com/en-ie/kb/319292
You can look up the directory that your application is installed in at runtime, using the Application.StartupPath property from System.Windows.Forms. This works for both applications and services. You should avoid hard-coding the path that you think your application is installed in, because the end user may have installed it somewhere else. You should also avoid using the current directory (i.e., opening the file by name only, without a specific path) because the user may be running your application with a different current directory.
Note that installutil does not make a copy of your service executable. So the service runs from the same directory that it was in when you installed it, and any files you place in that directory should still be there when the service is running.
I have developed an C# application that run very well in local.
But there is a problem when i put on the server.
The application use a DLL library (A.dll) in a point of the execution, this A.dll copy (or is create at the first execution) another DLL (B.dll) in the AppData/Local/TEMP directory. But when the A.dll try to load the B.dll an exception is threw:
system.invalidoperationexception: failed to load B.dll
I have tried to run as Administrator the main exe, but with no results. And i think that the permissions are ok, after all is the TEMP directory.
I have not access at the DLLs source, are libraries.
Anyone can suggest me any solution? Would i check better? Where?
Thank You All.
Have you checked that the system does not restrict execution of code from within the temp directory, e.g. using SRP or AppLocker?
This is usually set in group policy and is generally a sensible restriction these days to prevent things such as drive-by installers and cryptolocker malware.
You can verify this by trying to run an executable from %TEMP% and seeing if you get an access denied error.
If this is the case you can try relocating TEMP (as SRP usually defaults to locking you out of %USERPROFILE%\AppData\Local\Temp and moving the Temp folder will have the desired effect.
Hope this helps!
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.
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.
I've built a winforms app (C#) that will take a list of file paths, and copy those files (from a different VS solution) to a new location (In a folder the user specifies) in the same directory structure they currently exist on local file system.
I use the Path class, Directory class etc and everything works wonderfully...except when it reaches a file path that points to a DLL.
The DLLs I am trying to copy are a part of the other solution, and that solution is not currently open.
I have tried restarting computer to make sure visual studio isn't somehow hooking into that DLL even after the solution is closed.
The DLL in question can be copied by regular manual means (i.e. copy and paste shortcut).
So short of creating a batch file in the program, and running xcopy on that DLL path, I don't know of a way to get this to work.
From what I have found from google searches (which isn't much on this particular situation), File.Copy() should work..
Any help would be wonderful, even if it is a link to a duplicate question I may have over looked.
Thanks!
-The error message is: The process cannot access the file [insert file path] because it is being used by another process (The path is definitely correct also)
-Just downloaded and tried to search for the DLL name with Process Explorer.. I also ran a similar exe from command prompt to no avail. It claims nothing is using it. That's why I am utterly baffled by this. Also, I just checked the permissions and everything looks great (i.e. Full Control, owner effective permissions)
-It does not handle open files. It basically build the correct src and dest paths and does a File.Copy() on those. How would I go about handling open files? I'm sure I could figure out if it was open, but what would I do it it were open?
It is not complaining about the file you're trying to copy, it is complaining about the file that you're trying to overwrite with the copy. Lots of candidates for that, virus scanners always get very excited about new DLLs, for example. Or it is loaded into a process, the typical failure mode for trying to implement your own auto-updater.
You can rename the target file to make your copy succeed.
Are you in vista or win7? If so, Check your 'User Account Control Settings'. Sometimes this can interfere with .NET security options and prevent file operations that would otherwise work.
As well as Process Explorer, I would use Process Monitor also from Microsoft so you can see what is happening at the point of failure and allows you to see if anything else is accessing the dll.
Possible culprits are
the program you are running,
your antivirus package
a virus.
If the path it is complaining about is the destination path, then is is possible that the path is too long?
Also, when using Process Explorer, make sure you have enabled the option to show details for all processes and not just your own.
I just ran into this issue as well. I tried copying a .DLL from an FTP server to a local directory (replacing the existing one) and for the life of me I could not get it to work. Keeps giving me an 'Access Denied code: 5' Error.
I then realized that the .DLL on the FTP server was not marked as hidden while the .DLL I was trying to replace was marked as hidden.
Once I changed the local one to also be visible. I had no more issues.
So my solution is:
Make sure both files are visible.
Hope this helps someone