Running a C# FileSystemWatcher Service forever - c#

I've developed a service that watches one directory and then if a file is created or changed it will spin up another small c# script that will determine what to do with that file. I need this to be constantly listening to the directory at all times(even on start up of the server) because a file could be generated in this directory at literally any time.
I've searched and tried a few things but each time. The service will run then a file will be generated...then it launches the other C# script and does what it needs to with that file and then the service stops. So next time something happens in that directory nothing happens. Is there something I'm missing? How would I go about doing this?

So, your C# service will be stopped working after the first time that it runs your C# script?
I think it should related to your service configuration.
Checkout these links to see how you should create a service through C# :
http://www.codeproject.com/Articles/3938/Creating-a-C-Service-Step-by-Step-Lesson-I
http://www.codeproject.com/Articles/106742/Creating-a-simple-Windows-Service
http://www.codeproject.com/Articles/400697/WCF-Service-Creation-With-Csharp
I hope it helps you to find your mistake.
(If there's sth else, tell in comment)

Related

Looking to launch a local exe on a remote machine without having the resurces on the remote machine

So I have built some code, it's quite simple basically it stops all active input from keyboard and mouse until a text file of a certain name appears in the C:\Temp directory. It also has a manifest file to run it as administrator on start up.
So I found something that on the surface looks like it fulfils my needs of being able to do this task however upon running it I found out that the project has been compiled in x86 and does not run on my x64 machine. Here is the reference to the project if anyone would like to look into it, it's a very smartly designed piece of code that does an interesting objective. It also explains clearly enough what I am trying to accomplish.
So after implementing this (and failing) I have setup a couple other avenues to try, one is VBA through excel with the VBA copying itself to and from the machines in a list and running itself, then there is using VBS to write the entire code as a txt file on the target machine change the extension and then execute it remotely. I have just started researching these but I imagine the problems of running as an administrator amongst other things will crop up again to be dealt with. To be honest though I would really prefer to do this in C# only as that is the language I'm trying to go further in so I'm interested in this challenge. If anybody knows of a similar library of code or application I could look into to achieve what I'm trying I would appreciate being pointed in the right direction.
I would try and be more specific about what libraires/API's im trying to implement but the truth is I don't know what libraries I need to even interact with to get what I want. My goal is to have C# executable code on my machine and a tool that can run that executable on another machine.
Thanks
Thanks to the help in comments from #Nick.McDermaid I was able to correctly open and build the project I was trying to download. Unsure what caused the issue previously with me not being able to open and interact with the code but now I have it I shall pursue this avenue further to accomplish my goal.
As an addendum one other avenue I tried for executing code remotely was through VBS where I used
set svcproc=getobject("winmgmts:{impersonationLevel=impersonate}!\\"&MachineName & "\root\cimv2:win32_process")
scmd="""C:\Program Files\Internet Explorer\iexplore.exe"" -framemerging ""https://gifyu.com/images/Boo-Ghost-Gif.gif"""
'scmd="C:\Windows\notepad.exe"
iret=svcproc.create(scmd,null,null,pid)
set svcproc=nothing
to execute something that existed on the remote machine but I ran into a LOT of security policy issues where I could launch the process but I couldn't bring it to the foreground as the Malware tracker on the machine thought it was an attack and quashed it immediately.

Multiple executable accessing the same folder at the same time

We have a python application that checks a directory(C:\sample\folder) every 5 seconds, there's also this external application(.net app) that puts file into that same directory (C:\sample\folder).
Will there be any conflict when the two application access the same folder at the same time (accidentally)?
Conflicts like :
the external app wont be able to place a file because the python app is currently walking through that same directory?
It should be fine for the external app to create and write to a file. If the Python app is reading a file, the .NET app may not be able to write to it while Python is reading it, without both processes opening the file in a shareable way, however.
Likewise if the Python app is going to start reading the newly-created file, it may either find that it can't do so until the .NET app has finished writing to it, or it may read incomplete data. Again, changes would quite possibly be required to both processes to allow reading at all.
It's worth thoroughly testing all the poosibilities you're concerned about, possibly involving the creation of a "fake" external app which writes to a file very slowly, but opening it in the same way that the real one does.

Running file as scheduled task is not working properly

I've written a script in c# which I have to execute on a daily basis. It's something like an updater for my data base. So it pulls some data from the Active Directory using CSVDE, and saves that info into csv files in the same directory, then parses those files and updates my database if necessary. Obviously I don't want to run it manually every day, so instead I added a scheduled task. My problem is that it doesn't create the expected files, at least in the same path. If I run the script manually it works like a charm. Also after a scheduled run I tried to find those files by filename on the machine to check whether those were created in some other place, but no luck.
Do you have any idea why it not works the same way?
It was some issue with the credentials under the script ran. Solved now.

Updating MSMQ permissions on a private queue via C#

we are using some private MSMQ queues with our production system. Since implementing, we've had to update some stuff with the queues and re-create them to work with updated code. We have over 200 machines that need these updates so I'm working on a program that will be pushed via SMS to do this update.
What I'm noticing is that the only person that can run my program to do the update is the one that originally setup the private queue on that particular machine. Because of the number of machines, there were multiple admins that setup these queues.
Since not all of the employees still work here, this is causing me [my program] a problem. The permissions are not allowing the program to update everything that I need. I googled and found this link http://social.msdn.microsoft.com/Forums/hu-HU/msmq/thread/36a3d910-d533-4af3-86dc-498d00c68fef that shows how to update the permissions by modifying the file that is created for each of the queues. Great! It works when I manually navigate to that path and do the update. Now I want to do it programmatically via SMS push.
When trying to run this from my program, I get an error back saying the directory does not exist "C:\Windows\System32\msmq\storage\lqs". Huh? When I enter that path into Start->Run, it brings up the folder just fine. Well, breaking on the if (Directory.Exists(path)) part of my code definitely returns a false. If I remove all of the folders beyond System32, then the Directory.Exsts works just fine.
Why can't my program determine whether or not that msmq folder exists? I've tried "running as administrator" and it still returns false. What do I need to do to get that check to work?
Thanks
Edit: This is really weird, I have a FindAndReplace API that I wrote that will take in a path (file or directory), find text, replace text, etc. When I'm running that program from my Find And Replace GUI wrapper, it works just fine. But when I'm calling this API from my update program, it says that directory doesn't exist. This is really confusing. (I should probably remove the MSMQ tag because it has nothing to do with my question...)
Project + Properties, Build tab. Change the "Target platform" setting from x86 to AnyCPU. This lets you program run in 64-mode so the c:\windows\system32 directory search doesn't get redirected to c:\windows\syswow64.

Issue restarting a Windows service

Hi I made some changes in a windows service coding side(some class files related to that),,means i did coding to fetch version value from registry in that class files,,,,After
Your OnStart method is most probably hanging. Did you recently add any of that threading/timer code.
Is your tracer logging any information?
Can you write some debug code to log where you are getting to and what exceptions are being thrown?
So it looks like OnStart is throwing an exception (in SpoER.Init()?, and it has permissions problem as well), this is causing the service to try and stop straight away.
I would suggest following the exception information you have given to try and locate the problem. It would be even better to move the bulk of this code into a separate class so you can write a console app which shares the same code as the service. By using the console app you can easily debug it.
Now i got the mistake,,What actually i was doing is i am copying DLL created in the debug mode and copying into the Installer path..Actually what i need is i have to copy DLL obtained from the release mode and copy into the installer side

Categories