2

I am currently working on Windows Service to copy data from our security cameras as it is being written to the Google Drive directory on the computer for instant upload. The files are accessible immediately after creation by the provided playback software so we would like if possible to immediately copy the data stream, that way we have some video even if the recording is interrupted (the files are 10 minute time blocks).

I currently have a service created which can watch the directory, however I am having some difficulty determining the best way to watch these files. Since they are modified continuously for 10 minutes, I will receive a large number of changed events. I was hoping there might be a way that I can capture the initial creation and start streaming the data to a second file. My concern here is that I need to ensure that I don't overrun the recording stream.

If this isn't possible or relatively simple, then I will just have to detect when the file is no longer being written to by using some logic with the last write time, but I am looking for suggestions on what the best way to do this might be. I am aware of the solutions proposed Here, but I am unsure if they apply to the situation I am dealing with. There are a large number of files within sub-directories so trying to keep track of which files I have are no longer triggering events could get very messy. Does anyone have any suggestions for how to do either of these methods?

Community
  • 1
  • 1
Jared
  • 1,449
  • 2
  • 19
  • 40

3 Answers3

1

Hmmm... You could try using a timer... This way, you can limit when it fires

private Boolean TimeToCheck=false;
public static void Run()
{   Timer timer=new Timer(2000); //2 seconds
    FileSystemWatcher fileWatch=new FileSystemWatcher();
    fileWatch.Path="DirToWatch";
    fileWatch.Filter="fileToWatch";
    fileWatch.Changed += new FileSystemEventHandler(OnChanged);
    fileWatch.Created += new FileSystemEventHandler(OnChanged);
    fileWatch.Deleted += new FileSystemEventHandler(OnChanged);
    //If you want rename, you could use the rename event as well  fileWatch.Renamed += new RenamedEventHandler(OnRenamed);
    timer.Elapsed += new ElapsedEventHandler(timer_done);
    watcher.EnableRaisingEvents = true;
    timer.Enabled = true; // Enable it
}

private static void OnChanged(object source, FileSystemEventArgs e)
{
    if(TimeToCheck)
    {
        TimeToCheck=false;
        timer.Enabled = false; // Enable it
        //move the files
        timer.Enabled = true; // Enable it
    }

}
private static void OnRenamed(object source, RenamedEventArgs e)
{
    if(TimeToCheck)
    {
        TimeToCheck=false;
        timer.Enabled = false; // Enable it
        //move the files
        timer.Enabled = true; // Enable it
    }
}
private static void timer_done(object sender, ElapsedEventArgs e)
{
    TimeToCheck=true;
}
Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
0

Have you looked at the FileSystemWatcher object? If i'm understanding the question correctly, it may be something you may want to use.... If you were to put this security file within a certain directory, you could then use file.copy to move the updated security log into the google drive folder...

Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
  • The service I have implemented so far uses FileSystemWatcher. I am trying to determine the best way to handle the large number of events it will generate and determine when to move the file, and preferably as a stream as it is being written if possible. – Jared May 22 '12 at 16:44
0

You could try to do this but to be honest this seems like a hack and I'm skeptical that Windows has any supported method for doing what you're trying to do. Essentially you're trying to listen in on a write stream.

It sounds like whatever solution you're working with right now is a black box so accessing the stream directly probably isn't an option. However, there is another approach. I would look into how you can create a virtual drive with your app in windows. That way you can have the recording application writing to your virtual drive path which will allow you to handle the streams however you like. Which can include writing them to two separate locations at the same time. Both Google drive and some local storage of some kind for example.

Here's a StackOverflow question on how to create virtual drives that should get you started: C#: Create a virtual drive in Computer

Community
  • 1
  • 1
Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
  • The solution is definitely a black box, and a terrible one at that. If the recording software allowed me to specify a folder instead of a drive to write the data to, I wouldn't even have this problem. At the rate this is going the solution is beginning to look like a different video recorder is cheaper than spending a bunch of time trying to write and debug this service. Nevertheless, I will look into your suggestion. Thank you. – Jared May 22 '12 at 16:51
  • Well either way I wish you luck! – Spencer Ruport May 22 '12 at 16:53