The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. October 14, 2019, 06:38:28 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Directory watcher in .NET (managed C++) using XmlSerializer for data persistence  (Read 4352 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: April 26, 2007, 04:42:03 PM »

Code:
// dirwatcher.cpp : main project file.

#include "stdafx.h"

System::Void serialize( System::Xml::Serialization::XmlSerializer^ xs, System::DateTime^ timeLastWritten )
{

System::Xml::XmlTextWriter^ writer = ( gcnew System::Xml::XmlTextWriter( "tlw.xml", System::Text::Encoding::UTF8 ) );

writer->Formatting = System::Xml::Formatting::Indented;

try
{
xs->Serialize( writer, timeLastWritten );
}
catch( System::Exception^ e )
{
System::Console::WriteLine( e->InnerException->Message );
}

writer->Close();

}

int main(array<System::String ^> ^args)
{
System::String^ sTargetDir =
"C:\\some\\directory\\I\\want\\to\\watch";

System::IO::DirectoryInfo^ dir = ( gcnew System::IO::DirectoryInfo( sTargetDir) );

System::DateTime^ timeLastWritten = dir->LastWriteTime;

System::Xml::Serialization::XmlSerializer^ xs = ( gcnew System::Xml::Serialization::XmlSerializer( timeLastWritten->GetType() ) );

if( System::IO::File::Exists( "tlw.xml" ) )
{
System::Xml::XmlTextReader^ reader = ( gcnew System::Xml::XmlTextReader( "tlw.xml" ) );
System::DateTime^ savedTimeLastWritten = dynamic_cast<System::DateTime^>( xs->Deserialize( reader ) );
reader->Close();
if( savedTimeLastWritten->Equals( timeLastWritten ) )
return 0;
else
{
System::Windows::Forms::DialogResult confirmed = System::Windows::Forms::MessageBox::Show( "The Target Directory has Changed!",
"DirWatcher", System::Windows::Forms::MessageBoxButtons::OKCancel, System::Windows::Forms::MessageBoxIcon::Stop );

if( confirmed == System::Windows::Forms::DialogResult::OK )
{
serialize( xs, timeLastWritten );
}

}
return 0;
}

serialize( xs, timeLastWritten );

    return 0;
}
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 26, 2007, 05:10:30 PM »

TD -

I used a function years ago that popped an event when a DIR changed in any way (W2K)... is this the .NET replacement for such, or is this an augmentation? I'm a little lost in the code to know what it's doing at all...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: April 26, 2007, 06:41:58 PM »

hijack===
this is why i cant get going in .net
System::Windows::Forms::MessageBoxButtons::OKCancel
System::Xml::Serialization::XmlSerializer
and those arent even that bad...
god i really gotta just jump into it.
maybe i can really screw myself up and PHP.net
hijack===
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #3 on: April 26, 2007, 08:42:30 PM »

TD -

I used a function years ago that popped an event when a DIR changed in any way (W2K)... is this the .NET replacement for such, or is this an augmentation? I'm a little lost in the code to know what it's doing at all...

It just checks the last write time of the directory and serializes it to disk, then on subsequent runs it reads (and unserializes) the last write time on the directory to see if it's changed, if it has it shows an alert.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #4 on: April 26, 2007, 08:48:04 PM »

hijack===
this is why i cant get going in .net
System::Windows::Forms::MessageBoxButtons::OKCancel
System::Xml::Serialization::XmlSerializer
and those arent even that bad...
god i really gotta just jump into it.
maybe i can really screw myself up and PHP.net
hijack===

These can be reduced by using, for example;

using namespace System::Windows::Forms;

You can then refer to MessageBoxButtons::OKCancel

using namespace System::Xml::Serialization;

would allow you to change System::Xml::Serialization::XmlSerializer to just XmlSerializer

Not as bad as brainfuck  Nerd

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!