I have three classes, the FileManagerClass contains two events which are EventHandler<FileTransferEventArgs> FileMove and EventHandler<FileTransferEventArgs> FileMoveError.
I then have a custom EventArgs class named FileTransferEventArg that inherits from the base EventArgs class. I have a third class called FolderDirectoryModel that then listens for the events to be raised and responds to it with either a OnFileMove(object sender, FileTransferEventArg arg) or OnFileMoveError(object sender, FileTransferEventArg arg).
Overall, it looks like this:
class FileTransferManager
{
public event EventHandler<FileTransferEventArgs> FileMove;
public event EventHandler<FileTransferEventArgs> FileMoveError;
}
public class FileTransferEventArgs : EventArgs
{
string FileName { get; set; }
string Message { get; set; }
internal FileTransferEventArgs(FileModel file, string message)
{
this.FileName = file.FileName;
this.Message = message;
}
}
public class FolderDirectoryModel
{
void TransferFile(FileModel toBeTransfered, string destinationPath)
{
if (!File.Exists(toBeTransfered.FilePath))
{
File.Move(toBeTransfered.FilePath, destinationPath);
FileTransferEvents.FileTransferManager TransferAgent = new FileTransferEvents.FileTransferManager();
TransferAgent.FileMove += new EventHandler<FileTransferEventArgs>(OnFileMove);
}
else
{
FileTransferEvents.FileTransferManager TransferAgent = new FileTransferEvents.FileTransferManager();
TransferAgent.FileMoveError += new EventHandler<FileTransferEventArgs>(OnFileMoveError);
}
}
//Handler Methods
private void OnFileMoveError(object sender, FileTransferEventArgs args)
{
//what i want to happen
MessageBox.Show($"File {args.FileName} has not been moved succefully to {args.FilePath} because of....");
}
private void OnFileMove(object sender, FileTransferEventArgs args)
{
//what i want to happen
MessageBox.Show($"File {args.FileName} has been moved successfully to {args.FilePath}
}
}
My problem is that I would like to use the properties inside my FileTransferEventArgs class to display the file name that was moved, whether or not it was successful, and providing a reason that will be inside the message property.
However when I get every thing wired up I not able to view the data that is passed along. I am not sure if I am improperly using the events or I am trying to do something that is not possible.
The
FileMoveandFileMoveErrorevents must be placed in theFolderDirectoryModelclass.Events are triggered in the same class. Using the
Invokemethod.Usage
Event handlers are located in the same code where the
FolderDirectoryModelinstance is created and used.