This question already has answers here:
Notification when a file changes?
(3 answers)
Closed 8 years ago.
I have inherited application that, among many other things, has to watch if user writes/deletes text file into specific folder.
Currently, the application uses timer and polls after 5 seconds. I find this ineffective, and wish to improve this part of code.
My question is about existence of the .NET function that monitors changes in directory. Is there such function I can use to detect when a file is written/deleted in a specified folder?
Thank you.
Yes, you have the FileSystemWatcher class. It does exactly what you're looking for
Yes there is. I would suggest you take a look at the FileSystemWatcher class:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx
It's quite easy to set up, and it monitors for Win32 events, so is relatively inexpensive to use.
Related
This question already has answers here:
What is the correct way to create a single-instance WPF application?
(39 answers)
Closed 6 years ago.
I've been struggling with this for more than several hours and cannot think of a solution.
I have an application that can be started in this way:
TestApplication.exe ID={GUID} filename={filename}
If there is not an instance of the application with the same GUID, a new instance should be started with ID={GUID} and the specified file should be loaded in it.
If there is an instance of the application with the same GUID, the user should be asked if he wants to close the file he is working on and if he confirms it - then the new file specified should be opened in the running instance.
Any ideas how to implement this?
Use a Mutex. See the first answer of this question:
What is the correct way to create a single-instance application?
Any ideas how to implement this?
Yes. Question answered. You never ask us to show us our ideas.
Let's get real.
One way is by window title, but having the GUID there is seriously not optimal.
As such, read up on NAMED MUTEXES. You can use that one to identify a program already running.
Alternatively - and better given you must actually send a signal - named pipes. You can register a named pipe with the GUID. Fails: Already exists. THAT allows the new application to actually signal the old one and or send a shutdown command.
This question already has answers here:
Intercept windows open file
(4 answers)
Closed 7 years ago.
Is there an easy-to-implement method to detect whenever a user opens a file?(double click, right click, etc.)
I've read this but I think it only polls the file's lastaccess time. The main goal that I'm trying to achieve is whenever a user opens a file, the code picks up file name, location, size and all that good stuff.
I don't think I can ask this in any other way. I'm at a loss as to where to start.
Actually, the FileSystemWatcher from your linked question is the correct way to go. It does NOT poll anything, it listens to the OS events for file access. If there is no OS event, you cannot react to it.
This question already has answers here:
C# : Making an exe to not run directly
(4 answers)
Closed 7 years ago.
I want to have a WPF C# program launch a second WPF C# program, and prevent this second program from being launched any other way. Any ideas?
You could pass the 1st application's Process Id as a parameter, and have the 2nd application look up and validate the calling process.
Compile the second program as a dll, make the entry point internal, and have both applicatios share namespaces. Depending on your level of security, you might want to implement some handshake protocol between both applications.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to overlay an application over a full-screened program?
Is it possible in C# to inject a text to process (like fraps for example), but without using any .dll injections?
Thanks in advance for responses.
#update
"A text" means some fast refreshing labels or something, which will show informations e.g.:
Name:Test
Pos: x=123,y=456,z=0
Level: Unknown
Something.....
You can use automation to send keyboard actions and suchlike to another program. Otherwise if there is no exposed API then things look bleak. See this question for an overview on the methods you use to send keystrokes.
EDIT: What you're asking for is not injection, it's an overlay. What you're looking to do is take control of the display buffer so that your overlay always has a higher z-index than whatever is being rendered. Take a look at this answer
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Looking for C# code for detecting removable drive (usb flash)
How could the System.IO.FileSystemWatcher class be used to detect drives (e.g. F:) being connected/disconnected under windows? If this is not possible, what other event driven ways are possible (other than polling DriveInfo.GetDrives())?
Thanks in advance.
Take a look at this:
http://www.codeproject.com/KB/system/DriveDetector.aspx
I wrote a powershell module that uses a System.Management.ManagementEventWatcher and the WMI class Win32_VolumeChangedEvent to surface new events that you may register for within powershell covering device removal, addition etc. You should be able to figure out the relevant plumbing from this blog post of mine:
http://www.nivot.org/nivot2/post/2008/08/16/AutoMountunmountNewPSDrivesForRemovableDrivesAndNetworkSharesInPowerShellV2.aspx
You should be able to wire up an event for new drives in less than ten lines of C# using the methods I use in the above script.
Hope this helps.