Dillon More

Dillon More

  • NA
  • 6
  • 0

VB.net to C# conversion issues

Nov 12 2005 8:19 PM

Hello folks, I'm having problems running this code in C# as it was orignally in VB.Net ( http://www.knowdotnet.com/articles/filewatcherservice.html )

The URL above has the article that uses FileSystemWatcher class and Windows Service that needs to be running in the background in order for the FileSystemWatcher to watch the files on a specified path.  The code below is just a converted version of the VB.Net code found on the above URL.  Since my application is in C#, it's giving me quite a few errors. Can someone please look at it and help me out.  If you need full VB.Net code, you can look at the article above, and then use http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx  to convert it into C#. Thanks  very much in advance.

private void FileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
 {
   string FileTime;
   try {
     if (e.FullPath.ToUpper.IndexOf("FILEWATCHERFILES") > -1 || (e.FullPath.ToUpper.IndexOf("~WRD") > -1 & e.FullPath.ToUpper.IndexOf(".TMP") > -1)) {
return;
     }
   } catch {
return;
   }
   try {
     FileTime = FileDateTime(e.FullPath).ToString;
   } catch {
     FileTime = Now.ToString;
   }
   try {
     LogFilesAndErrors("TM:" + vbTab + FileTime + vbTab + "FN: " + vbTab + e.FullPath);
   } catch {
   }
 }

 private void FileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
 {
   string FileTime;
   try {
     if (e.FullPath.ToUpper.IndexOf("FILEWATCHERFILES") > -1 || (e.FullPath.ToUpper.IndexOf("~WRD") > -1 & e.FullPath.ToUpper.IndexOf(".TMP") > -1)) {
return;
     }
   } catch {
return;
   }
   try {
     FileTime = FileDateTime(e.FullPath).ToString;
   } catch {
     FileTime = Now.ToString;
   }
   try {
     LogFilesAndErrors("Deleted TM:" + vbTab + FileTime + vbTab + "FN: " + vbTab + e.FullPath);
   } catch {
   }
 }

 private void LogFilesAndErrors(string FullPath, string Time)
 {
   int i;
   int start;
   int h;
   string sLine;
   if (Time.Length == 0) {
     Time = Now.ToString;
   }
   sLine = FullPath;
   for (int i = 1; i <= 100; i++) {
     if (System.IO.File.Exists(TxtFilePath)) {
       try {
         h = FreeFile();
         FileOpen(h, TxtFilePath, OpenMode.Output, OpenAccess.Write, OpenShare.LockWrite);
         PrintLine(h, sLine);
         FileClose(h);
return;
       } catch {
       }
     } else {
       try {
         h = FreeFile();
         FileOpen(h, TxtFilePath, OpenMode.Append, OpenAccess.Write, OpenShare.LockWrite);
         PrintLine(h, sLine);
         FileClose(h);
return;
       } catch {
       }
     }
     start = Microsoft.VisualBasic.Timer;
     while (Microsoft.VisualBasic.Timer - start < 1) {
     }
   }
 }
}