473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Porting a FileSystemWatcher app from CosoleApplication to Windows Forms

Hello,

I have the following app. It works great as a console app. It tracks any file changes, renames, or errors:

Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.     {
  3.         FileSystemWatcher watcher = new FileSystemWatcher(@"C:\");
  4.  
  5.         watcher.NotifyFilter = (NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName);
  6.         watcher.IncludeSubdirectories = true;
  7.  
  8.         watcher.Changed += new FileSystemEventHandler(OnChanged);
  9.         watcher.Created += new FileSystemEventHandler(OnChanged);
  10.         watcher.Deleted += new FileSystemEventHandler(OnChanged);
  11.         watcher.Renamed += new RenamedEventHandler(OnRenamed);
  12.         watcher.Error += new ErrorEventHandler(OnError);
  13.  
  14.         watcher.EnableRaisingEvents = true;
  15.  
  16.         Console.WriteLine("Press 'Enter' to exit...");
  17.         Console.ReadLine();
  18.     }
  19.  
  20.     private static void OnChanged(object source, FileSystemEventArgs e)
  21.     {
  22.         WatcherChangeTypes changeType = e.ChangeType;
  23.         Console.WriteLine("File {0} {1}", e.FullPath, changeType.ToString());
  24.     }
  25.  
  26.     private static void OnRenamed(object source, RenamedEventArgs e)
  27.     {
  28.         WatcherChangeTypes changeType = e.ChangeType;
  29.         Console.WriteLine("File {0} {2} to {1}", e.OldFullPath, e.FullPath, changeType.ToString());
  30.     }
  31.  
  32.     private static void OnError(object source, ErrorEventArgs e)
  33.     {
  34.         Console.WriteLine("An error has occurred.");
  35.     }
  36.  
I'm trying to port the same app to windows forms.
But the following code does not display anything in textbox1. any ideas?

Expand|Select|Wrap|Line Numbers
  1. string str = "";
  2.  
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void Form1_Load(object sender, EventArgs e)
  9.         {
  10.             FileSystemWatcher watcher = new FileSystemWatcher(@"C:\");
  11.  
  12.             watcher.NotifyFilter = (NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName);
  13.             watcher.IncludeSubdirectories = true;
  14.  
  15.             watcher.Changed += new FileSystemEventHandler(OnChanged);
  16.             watcher.Created += new FileSystemEventHandler(OnChanged);
  17.             watcher.Deleted += new FileSystemEventHandler(OnChanged);
  18.             watcher.Renamed += new RenamedEventHandler(OnRenamed);
  19.             watcher.Error += new ErrorEventHandler(OnError);
  20.             watcher.EnableRaisingEvents = true;
  21.         }
  22.  
  23.         private void OnChanged(object source, FileSystemEventArgs e)
  24.         {
  25.             str.Insert(str.Length, (e.FullPath + " " + e.ChangeType.ToString() + "\n"));
  26.             textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = str; }));
  27.         }
  28.  
  29.         private void OnRenamed(object source, RenamedEventArgs e)
  30.         {
  31.             str.Insert(str.Length, (e.OldFullPath + " " + e.FullPath + " to " + e.ChangeType.ToString() + "\n"));
  32.             textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = str; }));
  33.         }
  34.  
  35.         private void OnError(object source, ErrorEventArgs e)
  36.         {
  37.             str.Insert(str.Length, ("An error has occurred." + "\n"));
  38.             textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = str; }));
  39.         }
  40.  
Any ideas at what I'm doing wrong?
Thanks
Jul 27 '10 #1
2 1909
hype261
207 100+
@Andraos
Is there a reason you are not just doing this?

Expand|Select|Wrap|Line Numbers
  1. textBox1.Text = str;
Jul 27 '10 #2
Hi Hype,

We can't directly assign 'str' to textbox1.Text. The following exception occurs:

"Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on."

Hence the <control>.Invoke() method.
Jul 27 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Christian Wilcox | last post by:
Does anyone know of any existing Python implementations of an XForms viewer? Christian
0
by: Lloyd Sheen | last post by:
I have created a FileSystemWatcher application that monitors a set of folder for changes. This works well as a windows app. I have copied the code from the windows app to a windows service and...
2
by: Greg Bacchus | last post by:
Hi, I'm getting an exception that really has me stumped. It's sporadic at best, it's only happened a handful of times. This particular time it happened when the user pressed 'Alt-S' to save the...
15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
6
by: Ayende Rahien | last post by:
Excetremely annoying problem, I've an application with a long startup time. So I created another form with my logo in it to as a splash screen. The splash screen is run from another thread and is...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
4
by: ljh | last post by:
I am trying to run some pretty simple code that monitors a folder for changes (copied the example at http://www.codeproject.com/dotnet/folderwatcher.asp). But, when I try and update a textbox...
0
by: gxl034000 | last post by:
Hi, I have been trying to use a .net Forms control in my webpage to open up an application(notepad) on the client. The control works fine when embedded in a windows form, but I keep getting a...
5
iknc4miles
by: iknc4miles | last post by:
Hi all, How would I go about making an array of controls such as textboxes for Windows Forms Programming using C++ on VC++ .NET 2003? I've tried porting the C# code from this site, but either I'm...
21
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.