WatchFileActivity Windows Workflow

Windows Workflow WatchFileActivity. C#. Activity , , . : , , . Activity . Activity, , , .
MS Visual Studio 2010 WCF + WorkFlow, Activity. WorkFlow, , Windows Form , : , .
1. WatchFileActivity.
Activity :
  • Path - .
  • IncludeSubdirectories - , .
  • Filter - , , .
[RequiredArgument]
public InArgument<string> Path { get; set; }

[RequiredArgument]
public InArgument<bool> IncludeSubdirectories { get; set; }

[RequiredArgument]
public InArgument<string> Filter { get; set; }
2. WatchFileActivity.
Activity Result .
[RequiredArgument]
public OutArgument<string> Result { get; set; }
3. WatchFileActivity.
WatchFileActivity NativeActivity :
/* 09.08.2012
1. Watch WatchFileActivity.
2. Activity , , .
3. Activity :
Path- .
IncludeSubdirectories- , 堠 .
Filter- , , .
4. Result .
*/
using System;
using System.Collections.Generic;
using System.Activities;
using System.Activities.Hosting;
using System.IO;
using ClassLibrary;

namespace ActivityLibrary
{
public class WatchFileActivity : NativeActivity
{
[RequiredArgument]
public InArgument<string> Path { get; set; }

[RequiredArgument]
public InArgument<bool> IncludeSubdirectories { get; set; }

[RequiredArgument]
public InArgument<string> Filter { get; set; }

[RequiredArgument]
public OutArgument<string> Result { get; set; }

private FileSystemWatcher fileSystemWatcher= null;
private Bookmark bookmark= null;
private FileSystemWatcherExtension extension= null;

protected override void Execute(NativeActivityContext context)
{
try
{
// Create FileSystemWatcher
fileSystemWatcher = new FileSystemWatcher(context.GetValue(Path));
fileSystemWatcher.IncludeSubdirectories = context.GetValue(IncludeSubdirectories);
fileSystemWatcher.Filter = context.GetValue(Filter);

// Create a bookmark and get a link to it
bookmark = context.CreateBookmark(ResumeFileCreatedBookmark,BookmarkOptions.MultipleResume);

// Get a reference to the class extension FileSystemWatcherExtension
extension =context.GetExtension<FileSystemWatcherExtension>();

// Let's get the two links - the Watcher and the Bookmark function in Start
extension.Start(fileSystemWatcher, bookmark);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

protected override bool CanInduceIdle
{
get { return true; }
}

// Call to create/estore bookmarks
public void ResumeFileCreatedBookmark(NativeActivityContext context, Bookmark bookmark, object obj)
{
Result.Set(context, (string)obj);
}
}
}
4. WatchFileActivity.
Activity FileSystemWatcherExtension, :
/*09.08.2012
FileSystemWatcherExtension WatchFileActivity.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Hosting;
using System.IO;

namespace ClassLibrary
{
public class FileSystemWatcherExtension : IWorkflowInstanceExtension
{
WorkflowInstanceProxy instance;
Bookmark bookmark;

public void SetInstance(WorkflowInstanceProxy instance)
{
this.instance = instance;
}

IEnumerable<object> IWorkflowInstanceExtension.GetAdditionalExtensions()
{
yield break;
}

public void Start(FileSystemWatcher fileSystemWatcher, Bookmark bookmark)
{
// Get a reference to a bookmark
this.bookmark = bookmark;

// Create a handler to create, rename, delete files
fileSystemWatcher.Created += new FileSystemEventHandler(FileCreated);
fileSystemWatcher.Renamed += new RenamedEventHandler(FileRename);
fileSystemWatcher.Deleted += new FileSystemEventHandler(FileCreated);

// Enable the reaction to changes to files and directories
fileSystemWatcher.EnableRaisingEvents = true;

Console.WriteLine("Start Watcher ...");
}

// 1. Handler when a file is created, that is, at first he was going to work!
void FileCreated(object sender, FileSystemEventArgs e)
{
instance.BeginResumeBookmark(bookmark, e.FullPath, CompleteResume, null);
Console.WriteLine("File Created: " + e.FullPath + " " + e.ChangeType);
}

// Handler when a file is renamed.
void FileRename(object sender, RenamedEventArgs e)
{
instance.BeginResumeBookmark(bookmark, e.FullPath, CompleteResume, null);
Console.WriteLine("Rename File:" + e.FullPath + " " + e.OldFullPath);
}

// Handler when a file is deleted.
void FileDeleted(object sender, FileSystemEventArgs e)
{
instance.BeginResumeBookmark(bookmark, e.FullPath, CompleteResume, null);
Console.WriteLine("Deleted File: " + e.FullPath + " " + e.ChangeType);
}

// 2. The second phase at the end of a bookmark
void CompleteResume(IAsyncResult ar)
{
var result = instance.EndResumeBookmark(ar);
}
}
}
5. WatchFileActivity.
, . , .
/* 09.08.2012
Activity WatchFile
:
1. , - .
2. .
3. ,
Activity : , , .
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientTestWatchActivity;
using System.IO;

namespace ClientTestWatchActivity
{
public partial class Form1 : Form
{
FileStream file;

public Form1()
{
InitializeComponent();
}

// .
private void button1_Click(object sender, EventArgs e)
{
try
{
//
string fileName = textBox2.Text;
// ?
if (String.IsNullOrEmpty(fileName)) throw new ArgumentOutOfRangeException("Name File: ", "Empty Value");

// , ?
const string caption = "Delete File";
if (File.Exists(fileName))
{
var result = MessageBox.Show(fileName, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// -
if (result == DialogResult.Yes) File.Delete(fileName);
}

file = File.Open(fileName, FileMode.Create);
byte[] bytes = UnicodeEncoding.Unicode.GetBytes(textBox1.Text);
file.Write(bytes, 0, bytes.Length);
file.Flush();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error ");
}
finally
{
if(!(file == null))
file.Close();
}
}

//
private void button2_Click(object sender, EventArgs e)
{
try
{
//
string path = textBox2.Text;
//
string newPath = textBox3.Text;

// ?
if (String.IsNullOrEmpty(path)) throw new ArgumentOutOfRangeException("Name File: ", "Empty Value");
// ?
if (String.IsNullOrEmpty(newPath)) throw new ArgumentOutOfRangeException("New Name File: ", "Empty Value");

// , , ?
const string caption = "Delete File";
if (File.Exists(newPath))
{
var result = MessageBox.Show(newPath, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// -
if (result == DialogResult.Yes) File.Delete(newPath);
}

File.Move(path, newPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error ");
}
finally
{
if (!(file == null))
file.Close();
}
}

// - .
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
textBox3.Enabled = true;
button1.Enabled = false;
button2.Enabled = true;
}
else
{
textBox3.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
}
}
}
}

Visual Studio 2010 : Activity, Workflow Windows Form .
. C , .
09.08.2012 .

© Argument Ltd, 2017 ,  : 8-921-215-45-70,   e-mail