Windows service running as local service - input files? - c#

I have a windows service program running in C#. I have configured to run it as a local service with my config files stored in \bin\release\config\configvalues.txt. But, it does not recognize this file path and throws the "System.UnauthorizedAccessException".
I believe its looking for the files in the System32 folder and since it does not have the privilege, throws out the exception. For the workaround, my service is running with "local system" to recognize the System32\config folder. To run as a local service, which file path should the config folder be available?

Check the location of your project folder. Most likely, you'll find that it is inside your User profile folders, which the local service account does not have access to.
Deploy your project to a folder outside of any User profiles and you'll have better luck.

Related

GetCurrentDirectory() returns local path after deployed on server

Requirement:
In my .Net Core console application, I use Directory.GetParent(Directory.GetCurrentDirectory()) to retrieve the current parent folder of where my template file is stored.
Issue:
After deploying on server, when I manually run the exe by double clicking it works fine. But when I try to run it with batch or task scheduler, the above statement return by local computers path, instead of the path on server.
Server Details:
OS: Windows Server 2012 R2 Standard
Error Stack:
Error Message : Could not find document StackTrace : at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
at DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
at CV_Export.Program.SearchAndReplace(String TemplatePath, String NewDocumentPath) in C:\Siddharth\Demo Codes.Net Core Demo\CV Export with API\CV_Export\Program.cs:line 163 TIme : 7/21/2021 4:41:53 PM
This error was generated when I tried to run the exe through batch and the path on the error is of my local system.
Thank you in advance for your help.
By using Directory.GetParent(App.Domain.CurrentDomain.BaseDirectory) you can provide the relative path to the base directory of your application.
When you are using Directory.GetCurrentDirectory() you are providing the path from where you are running the application, which can be for instance C:\Windows\System32, and the parent directory will be C:\Windows instead of the parent directory of your .net application.
GetCurrentDirectory()
Gets the current working directory of the application.
The working directory is an environment concept and is affected by where and how the application is executed, not where the file is logically located.
You can avoid this by using App.Domain.CurrentDomain.BaseDirectory() instead, that will force lookups for these resources to always be relative to the executable.
BaseDirectory vs CurrentDirectory in C#
NOTE:
In many cases it is still beneficial to use GetCurrentDirectory() as that allows your executable to be installed in a central location, but to allow different user or security contexts to access a different set of files.
If you stick with GetCurrentDirectory() here's some advice on how to configure it from the calling context:
Batch Files / Scripts
When executing from a batch file, the current environment context will default to be operating in the working directory that applies to the batch folder.
So in your batch, you need to change the working directory to the folder the exe is in first, if you used a UNC path or full path to access the exe, then it won't be running in that directory, instead if you use a cd command to change the directory first that will often do the trick.
Task Scheduler
Specifying the running directory for Scheduled Tasks using schtasks.exe
If you are using the GUI, then you can deliberately set the working folder via Start In: https://stackoverflow.com/a/27401164/1690217
Shortcuts
Similar to Scheduled Tasks, in shortcuts you can also specify the working directory:

MVC4: Extract whole website files while running

I want to update my website by extracting a zip file which contains whole files of the website itself. Apparently the system does not allow me to do this as such folders are protected:
Error while extrating: Access to the path
'C:\WebsiteFolder\Website\bin\' is denied.
I also think that changing the dlls will restart the server while extracting. Is there a way to do this? To extract all files then restarts the server after it finishes. Also the web.config might be overwritten while extracting the zip file.
Does putting offline.htm help?
This is because IIS is using files within the bin directory, and the AppPool is likely running as system, or an administrator account.
Stopping IIS (or the app pool) will enable you to replace the files.
I also think that changing the dlls will restart the server while extracting
Yes, it will restart the app pool when you change these files

How to attach file to windows service?

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.

Deployed VS 2010 WPF Application Cannot Update Log Files Unless in Administrator Mode

I have a WPF Application that uses nLog to write out log files. On my machine it writes out the log file successfully.
I have created a deployment project, and generate an install, which installs it into program files . The install works successfully, but when the application is run, the log file generated by nLog is not created. No exception is thrown.
I am running Windows 7.
The log file is being created in the same directory as the executable.
If I run the deployed application in Administrator mode, the log file is created successfully.
How can I get past this? Would signing the executable help? Do I really need this to run in admin mode?
The problem is that you need to log to a different location. You should not write to C:\Program Files from your application. Instead, move your log files to something like %APPDATA%\Your Company\Your Application\Logs.
By using %APPDATA%, your application can run on different versions of Windows, and if they change the structure of the standard directories (like between XP and Vista), your application will log to the correct location still.
The Program Files directory is properly locked down. Change the application to write the log file to an unrestricted location. (I prefer the all users' application data directory.)
What others have said: the program files directory is locked down. You need to use another folder location to log towards.
I have had great success logging to the UserProfile special folder so that my logs wind up in this folder structure:
C:\Users\CurrentUser\My Docuements\Company Name\ApplicationName\
See this link for some useful information on special folderes: http://en.wikipedia.org/wiki/Special_Folders
%USERPROFILE%\Documents

Creating folders inside LocalLow through Installer

I am developing deployment project for Vista. In Vista inside AppData folder there are Local, LocalLow and Roaming folders. What I want from installer is to create folder 'Data' inside LocalLow folder and put there file data.xml (AppData\LocalLow\Data\data.xml). Installer should make this operation for all existing user accounts.
How can I achieve this?
This is a screenshot of setup project('Data' folder configuration) which is not working:
Attached example creates the following path: \AppData\Roaming\LocalLow\Data\data.xml
I think a much better approach is to store the xml file in the application's install directory, then, when the application starts up, copy the file to the appropriate directory.
The primary issue is this: what if a user that wasn't on the machine when it was installed launches the application?
Since your installer didn't copy the file to their directory (because it didn't exist), your application would either have to do something or fail anyway.

Categories

Resources