theniteowl
Programmer
I have a windows forms application and am trying to add a FileSystemWatcher routine into it so that it will update a list of files in a ListView box.
I have the watcher working in the application but now when an event is triggered by a change in the folder I want to call the fillListView routine in my form application code but do not know how to make the call to another class.
I am just learning C# and am probably missing some fundamentals so please bear with me.
From OnChanged in Program.cs I am trying to execute fillListView in AnnuityQtrStmtFTP.cs
The following code is under Program.cs
This code is under AnnuityQtrStmtFTP.cs
At my age I still learn something new every day, but I forget two others.
I have the watcher working in the application but now when an event is triggered by a change in the folder I want to call the fillListView routine in my form application code but do not know how to make the call to another class.
I am just learning C# and am probably missing some fundamentals so please bear with me.
From OnChanged in Program.cs I am trying to execute fillListView in AnnuityQtrStmtFTP.cs
The following code is under Program.cs
Code:
using System;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Threading;
public class FileInputMonitor
{
private FileSystemWatcher fileSystemWatcher;
string folderToWatchFor = WindowsFormsApplication2.frmVendorFileHandling.FTPPath;
public FileInputMonitor()
{
fileSystemWatcher = new FileSystemWatcher(folderToWatchFor);
fileSystemWatcher.EnableRaisingEvents = true;
// Instruct the file system watcher to call the FileCreated method
// when there are files created at the folder.
fileSystemWatcher.Created += new FileSystemEventHandler(OnChanged);
fileSystemWatcher.Deleted += new FileSystemEventHandler(OnChanged);
}
private void OnChanged(Object sender, FileSystemEventArgs e)
{
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
// fillListView(lvFTPFolder, FTPPath);
}
}
namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FileInputMonitor fileInputMonitor = new FileInputMonitor();
//Console.Read();
Application.Run(new frmVendorFileHandling());
}
}
}
This code is under AnnuityQtrStmtFTP.cs
Code:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class frmVendorFileHandling : Form
{
public static string StagingPath = ConfigurationManager.AppSettings.Get("StagingPath");
public static string FTPPath = ConfigurationManager.AppSettings.Get("FTPPath");
string ZipBackupPath = ConfigurationManager.AppSettings.Get("ZipBackupPath");
string INIPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "filepaths.ini");
public frmVendorFileHandling()
{
InitializeComponent();
fillListView(lvStagingFolder, StagingPath);
fillListView(lvFTPFolder, FTPPath);
fillListView(lvZIPBackupFolder, ZipBackupPath);
}
public void fillListView(ListView listview, string p)
{
string Msg = "";
DirectoryInfo sourceInfo = new DirectoryInfo(p);
FileInfo[] sFiles = sourceInfo.GetFiles("*.*");
try
{
foreach (FileInfo file in sFiles)
{
try
{
if (file.Name != "")
{
ListViewItem item = new ListViewItem(file.Name); // Create the item variable and assign the file name as the primary value
item.SubItems.Add(file.LastWriteTime.ToString("MM/dd/yyyy H:mm:ss")); // Add the last write time to the item as a subitem value
item.SubItems.Add(file.Length.ToString()); // Repeat as needed for each subitem - could even build an inner loop if needed
listview.Items.Add(item); // Add the item variable to the ListView
}
}
catch (UnauthorizedAccessException UnAuthTop)
{
Msg = Msg + UnAuthTop.Message;
}
}
}
catch (DirectoryNotFoundException)
{
Msg = Msg + "Error: Could not find Staging folder - " + StagingPath + Environment.NewLine;
}
catch (UnauthorizedAccessException)
{
Msg = Msg + "Error: Access not allowed at - " + StagingPath + Environment.NewLine;
}
catch (PathTooLongException)
{
Msg = Msg + "Error: Directory path is too long." + Environment.NewLine;
}
if (string.IsNullOrEmpty(Msg) == false)
{
MessageBox.Show(Msg);
}
}
private void btnMoveStagingtoFTP_Click(object sender, EventArgs e)
{
string Msg = "";
if (lvStagingFolder.CheckedIndices.Count > 0)
{
foreach (ListViewItem item in lvStagingFolder.Items)
{
if (item.Checked)
{
string sourceFile = Path.Combine(StagingPath, System.IO.Path.GetFileName(item.Text));
string destinationFile = Path.Combine(FTPPath, item.Text);
if (System.IO.File.Exists(sourceFile))
{
if (System.IO.File.Exists(destinationFile))
{
Msg = Msg + "File " + item.Text + " already exists in destination folder." + Environment.NewLine;
}
else
{
System.IO.File.Move(sourceFile, destinationFile);
Msg = Msg + item.Text + " has been moved to FTP folder." + Environment.NewLine;
}
}
else
{
Msg = Msg + "File " + sourceFile + " no longer exists in the STAGING folder, it may have been manually deleted." + Environment.NewLine;
}
lvStagingFolder.Items.Remove(item);
}
}
if (string.IsNullOrEmpty(Msg) == false)
{
MessageBox.Show(Msg);
lvFTPFolder.Items.Clear();
fillListView(lvFTPFolder, FTPPath);
}
}
}
private void btnDeleteSelected_Click(object sender, EventArgs e)
{
string Msg = "";
if (lvStagingFolder.CheckedIndices.Count > 0)
{
foreach (ListViewItem item in lvStagingFolder.Items)
{
if (item.Checked)
{
string sourceFile = Path.Combine(StagingPath, System.IO.Path.GetFileName(item.Text));
if (System.IO.File.Exists(sourceFile))
{
System.IO.File.Delete(sourceFile);
Msg = Msg + item.Text + " has been deleted from the Staging folder." + Environment.NewLine;
}
lvStagingFolder.Items.Remove(item);
}
}
if (string.IsNullOrEmpty(Msg) == false)
{
MessageBox.Show(Msg);
}
}
}
private void btnFTPtoStaging_Click(object sender, EventArgs e)
{
string Msg = "";
if (lvFTPFolder.CheckedIndices.Count > 0)
{
foreach (ListViewItem item in lvFTPFolder.Items)
{
if (item.Checked)
{
string sourceFile = Path.Combine(FTPPath, System.IO.Path.GetFileName(item.Text));
string destinationFile = Path.Combine(StagingPath, item.Text);
if (System.IO.File.Exists(sourceFile))
{
if (System.IO.File.Exists(destinationFile))
{
Msg = Msg + "File " + item.Text + " already exists in destination folder." + Environment.NewLine;
}
else
{
System.IO.File.Move(sourceFile, destinationFile);
lvFTPFolder.Items.Remove(item);
Msg = Msg + item.Text + " has been moved to the STAGING folder." + Environment.NewLine;
}
}
else
{
Msg = Msg + sourceFile + " has already been sent to the vendor or was manually deleted from the folder." + Environment.NewLine;
lvFTPFolder.Items.Remove(item);
}
item.Checked = false;
}
}
if (string.IsNullOrEmpty(Msg) == false)
{
MessageBox.Show(Msg);
lvStagingFolder.Items.Clear();
fillListView(lvStagingFolder, StagingPath);
}
}
}
private void btnReSendtoVendor_Click(object sender, EventArgs e)
{
string Msg = "";
if (lvZIPBackupFolder.CheckedIndices.Count > 0)
{
foreach (ListViewItem item in lvZIPBackupFolder.Items)
{
if (item.Checked)
{
string sourceFile = Path.Combine(ZipBackupPath, System.IO.Path.GetFileName(item.Text));
string destinationFile = Path.Combine(FTPPath, item.Text);
if (System.IO.File.Exists(sourceFile))
{
if (System.IO.File.Exists(destinationFile))
{
Msg = Msg + "File " + item.Text + " already exists in destination folder." + Environment.NewLine;
}
else
{
System.IO.File.Copy(sourceFile, destinationFile);
Msg = Msg + item.Text + " has been copied to FTP folder." + Environment.NewLine;
}
}
else
{
Msg = Msg + "File " + sourceFile + " no longer exists in the ZIP_BACKUP folder and may have been manually deleted." + Environment.NewLine;
lvZIPBackupFolder.Items.Remove(item);
}
item.Checked = false;
}
}
if (string.IsNullOrEmpty(Msg) == false)
{
MessageBox.Show(Msg);
lvFTPFolder.Items.Clear();
fillListView(lvFTPFolder, FTPPath);
}
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
lvStagingFolder.Items.Clear();
fillListView(lvStagingFolder, StagingPath);
lvFTPFolder.Items.Clear();
fillListView(lvFTPFolder, FTPPath);
lvZIPBackupFolder.Items.Clear();
fillListView(lvZIPBackupFolder, ZipBackupPath);
}
}
}
At my age I still learn something new every day, but I forget two others.