Hello duds
I'm new tro C# and working on a solution to read logs and send it as SMS.
I wrote the SMS integration and sending code and it's working.
the plan was to use PowerShell script to read those log files and i got some issues
now i need your help to user FileWatcher to get the log file once created and get the content from it into a vraibale
can any one help
Amit GuptaPosted Feb 18, 2018, 8:19 AM
System.IO.StreamReader file = new System.IO.StreamReader(@"d:\test.txt");
Ahmed ElkhoulyPosted Feb 18, 2018, 6:28 AM
using System;
using System.IO;
public class FileWatcher
{
public static void Main(string[] args)
{
// If a directory is not specified, exit program.
if (args.Length != 1)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: FileWatcher.exe");
return;
}
try
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[0];
// Watch both files and subdirectories.
watcher.IncludeSubdirectories = true;
// Watch for all changes specified in the NotifyFilters
//enumeration.
watcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.DirectoryName |
NotifyFilters.FileName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Security |
NotifyFilters.Size;
// Watch all files.
watcher.Filter = "*.*";
// Add event handlers.
watcher.Created += new FileSystemEventHandler(OnChanged);
//Start monitoring.
watcher.EnableRaisingEvents = true;
//Do some changes now to the directory.
//Create a DirectoryInfo object.
DirectoryInfo d1 = new DirectoryInfo(args[0]);
//Create a new subdirectory.
d1.CreateSubdirectory("mydir");
//Create some subdirectories.
d1.CreateSubdirectory("mydir1\\mydir2\\mydir3");
//Move the subdirectory "mydir3 " to "mydir\mydir3"
Directory.Move(d1.FullName + "\\mydir1\\mydir2\\mydir3",
d1.FullName + "\\mydir\\mydir3");
//Check if subdirectory "mydir1" exists.
if (Directory.Exists(d1.FullName + "\\mydir1"))
{
//Delete the directory "mydir1"
//I have also passed 'true' to allow recursive deletion of
//any subdirectories or files in the directory "mydir1"
Directory.Delete(d1.FullName + "\\mydir1", true);
}
//Get an array of all directories in the given path.
DirectoryInfo[] d2 = d1.GetDirectories();
//Iterate over all directories in the d2 array.
foreach (DirectoryInfo d in d2)
{
if (d.Name == "mydir")
{
//If "mydir" directory is found then delete it recursively.
Directory.Delete(d.FullName, true);
}
}
// Wait for user to quit program.
Console.WriteLine("Press \'q\' to quit the sample.");
Console.WriteLine();
//Make an infinite loop till 'q' is pressed.
while (Console.Read() != 'q') ;
}
catch (IOException e)
{
Console.WriteLine("A Exception Occurred :" + e);
}
catch (Exception oe)
{
Console.WriteLine("An Exception Occurred :" + oe);
}
}
// Define the event handlers.
public static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed.
Console.WriteLine("{0}, with path {1} has been {2}", e.Name, e.FullPath, e.ChangeType);
//Readafile();
}
private static void Readafile()
{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(@"d:\test.txt");
while ((line = file.ReadLine()) != null)
{
System.Console.WriteLine(line);
counter++;
}
file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();
}
}
Amit GuptaPosted Feb 18, 2018, 3:53 AM