Hi everyone,

It might sound simple but I am having nightmares with this function

What is the best way to delete a file without the machine complaining that i cannot acces the file because it is in use by another process? Is this a bug in the .net framework? Is it my crappy code? Is there a workaround?

inputfile.Close();
File.Delete(dir + "\\" + Files[j].ToString());

Thanks in advance.

Catch the exception

try {
    File.Delete(dir + "\\" + Files[j].ToString());
} catch (Exception e) {
}

As for why it's saying that something is accessing it, that's because it is. Just because you close a file doesn't mean the system releases the handle on it (and MS recomends that you call Dispose, not Close for what it is worth), it can take a while before it does.

I have that.

try
                            {
                                inputfile.Close();
                                File.Delete(dir + "\\" + Files[j].ToString());
                            }
                            catch (System.Exception FileError)
                            {
                                File.AppendAllText(@"C:\Gasper\FileMoveException.txt", FileError.Message);
                            }

It will take a while to dispose as well, then the service will fall over again.

How Would you change this service?

using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading;
using Lextm.SharpSnmpLib;
using Mono.Options;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;
using System.Net.Mail;

namespace AptraSNMPMessenger
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
        private Thread _thread;
        protected override void OnStart(string[] args) // Spawn a new thread for the mere purpose of doing the job
        {
            try
            {
              // var dir = @"C:\Gasper\Netcool";
              //  if (!System.IO.Directory.Exists(dir))
              //      System.IO.Directory.CreateDirectory(dir);
                _thread = new Thread(WorkerThreadFunc);
                _thread.Name = "My Worker Thread";
                _thread.IsBackground = true;
                _thread.Start();
            }
            catch (System.Exception ThreadStart)
            {
                File.AppendAllText(@"C:\Gasper\ThreadStart.txt", ThreadStart.Message);
                Email(@"D:\Gasper\ThreadStart.txt");
            }
        }

        void WorkerThreadFunc()
        {
            try
            {
                while (!_shutdownEvent.WaitOne(0))
                {
                    try
                    {
                        string TicketStatus = "";
                        string ATMID = "";
                        string StatusCode = "";
                        string EventDescription = "";
                        string DateofEvent = "";
                        string TimeofEvent = "";
                        string Record = "";
                        string ActionCode = "";
                        string temp = "";
                        string Ticket = "";

                        List<FileInfo> Files = new List<FileInfo>();
                        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(ConfigurationSettings.AppSettings["WatchPath"]);

                        foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
                        {// Gets all files in the directory
                            Files.Add(f);
                        }

                        for (int j = 0; j < Files.Count; j++)
                        {// Loop to make sure that we running through all files, if there are X number files in the folder, then this loop will run 20 times
                            int i = 0;
                            System.IO.StreamReader inputfile = new System.IO.StreamReader(dir + "\\" + Files[j].ToString());
                            do
                            {// extract data from each file and store it in variables to send to the SNMP function later
                                Record = inputfile.ReadLine();
                                if (Record == null)
                                    break;
                                if (Record.Contains("UPDATE"))
                                {
                                    TicketStatus = "ENTRYUPDATE";
                                }
                                if (Record.Contains("OPEN"))
                                {
                                    TicketStatus = "ENTRYOPEN";
                                }
                                if (Record.Contains("CLOSE"))
                                {
                                    TicketStatus = "ENTRYCLOSE";
                                }
                                if (Record.Contains("ATMID") && i == 0)
                                {
                                    ATMID = Record.Remove(0, 8);
                                    i++;
                                }
                                if (Record.Contains("CODE") && i == 1)
                                {
                                    StatusCode = Record.Remove(0, 7);
                                    i++;
                                }
                                if (Record.Contains("DESCRIPTION") && i == 2)
                                {
                                    EventDescription = Record.Remove(0, 14);
                                    i++;
                                }
                                if (Record.Contains("START"))
                                {
                                    temp = Record.Remove(0, 7);
                                    TimeofEvent = temp.Remove(0, 11);
                                    DateofEvent = temp.Substring(1, 10);
                                    DateofEvent = DateofEvent.Replace("/", "");
                                }
                                if (Record.Contains("TKT"))
                                {
                                    Ticket = Record.Remove(0, 7);
                                }
                                if (Record.Contains("Action") && i == 3)
                                {
                                    ActionCode = Record.Remove(0, 14);
                                    break; // Break because we have all the information that we need
                                }
                            } while (Record != null);

                            try
                            {// Take those Variables and send it to my SNMP function
                                inputfile.Close();
                                SNMPMessageSend(ATMID, StatusCode, ActionCode, EventDescription, Ticket, DateofEvent, TimeofEvent, TicketStatus);
                                SafeDelete(dir + "\\" + Files[j].ToString()); // Try to delete the file
                            }
                            catch (System.Exception SendCloseDelete)
                            {
                                using (StreamWriter writer = File.AppendText((@"D:\Gasper\SendCloseDeleteException.txt")))
                                {
                                    writer.WriteLine(SendCloseDelete.Message);
                                }

                                Email(@"D:\Gasper\SendCloseDeleteException.txt");
                            }
                        }
                        System.Threading.Thread.Sleep(5000);// Sleep for 5 seconds
                    }
                    catch (System.Exception OuterException)
                    {
                        using (StreamWriter writer = File.AppendText((@"D:\Gasper\OuterException.txt")))
                        {
                            writer.WriteLine( OuterException.Message);
                        }

                        Email(@"D:\Gasper\OuterException.txt");
                    }
                }// the X number files are complete, so now we can read the next number of files in the folder and repeat the process
            }
            catch (System.Exception SNMPError)
            {
                using (StreamWriter writer = File.AppendText((@"D:\Gasper\SNMPError.txt")))
                {
                    writer.WriteLine(SNMPError.Message);
                }

                Email(@"D:\Gasper\SNMPError.txt");
                WorkerThreadFunc(); // this will ensure that the Service keeps running.
            }
        }

        public bool SafeDelete(string path)
        {
            while (true)
            {
                try
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                        return true; // if the file was successfully deleted, then we can go to my WorkerThreadFunc method
                    }
                }
                catch (Exception ex)
                {
                    Thread.Sleep(100);  // else, if it did not delete, then go through this loop until it gets deleted
                    // Log if desired
                }
            }
        }

        private static void SNMPMessageSend(string ATMID, string StatusCode, string ActionCode,
               string EventDescription, string Ticket, string DateofEvent, string TimeofEvent, string TicketStatus)
        {
            try
            {
                IPAddress address = IPAddress.Parse(ConfigurationSettings.AppSettings["IP"]); // Prod IP in config file
                int port = int.Parse(ConfigurationSettings.AppSettings["Port"]); // Port in config file
                List<Variable> objects = new List<Variable>();

                // (1) add source system
                ObjectIdentifier oidSourceSystem = new ObjectIdentifier(ConfigurationSettings.AppSettings["oidSourceSystem"]);
                ISnmpData dataSourceSystem;
                dataSourceSystem = new OctetString(ConfigurationSettings.AppSettings["dataSourceSystem"]);
                Variable SourceSystem = new Variable(oidSourceSystem, dataSourceSystem);
                objects.Add(SourceSystem);

                // (2) add ATM ID
                ObjectIdentifier oidATMId = new ObjectIdentifier(ConfigurationSettings.AppSettings["oidATMId"]);
                ISnmpData dataATMId;
                dataATMId = new OctetString(ATMID);
                Variable ATMId = new Variable(oidATMId, dataATMId);
                objects.Add(ATMId);

                // (3) add ATM status code
                ObjectIdentifier oidATMStatusCode = new ObjectIdentifier(ConfigurationSettings.AppSettings["oidATMStatusCode"]);
                ISnmpData dataATMStatusCode;
                dataATMStatusCode = new OctetString(StatusCode);
                Variable ATMStatusCode = new Variable(oidATMStatusCode, dataATMStatusCode);
                objects.Add(ATMStatusCode);

                // (4) add ATM action code
                ObjectIdentifier oidATMActionCode = new ObjectIdentifier(ConfigurationManager.AppSettings["oidATMActionCode"]);
                ISnmpData dataATMActionCode;
                dataATMActionCode = new OctetString(ActionCode);
                Variable ATMActionCode = new Variable(oidATMActionCode, dataATMActionCode);
                objects.Add(ATMActionCode);

                // (5) add event description
                ObjectIdentifier oidDescription = new ObjectIdentifier(ConfigurationManager.AppSettings["oidDescription"]);
                ISnmpData dataDescription;
                dataDescription = new OctetString(EventDescription);
                Variable Description = new Variable(oidDescription, dataDescription);
                objects.Add(Description);

                // (6) add ticket nr
                ObjectIdentifier oidTicketNr = new ObjectIdentifier(ConfigurationManager.AppSettings["oidTicketNr"]);
                ISnmpData dataTicketNr;
                dataTicketNr = new OctetString(Ticket);
                Variable TicketNr = new Variable(oidTicketNr, dataTicketNr);
                string Tick = Ticket.ToString();
                objects.Add(TicketNr);

                // (7) add event date
                ObjectIdentifier oidEventDate = new ObjectIdentifier(ConfigurationManager.AppSettings["oidEventDate"]);
                ISnmpData dataEventDate;
                dataEventDate = new OctetString(DateofEvent);
                Variable EventDate = new Variable(oidEventDate, dataEventDate);
                objects.Add(EventDate);

                // (8) add event time
                ObjectIdentifier oidEventTime = new ObjectIdentifier(ConfigurationManager.AppSettings["oidEventTime"]);
                ISnmpData dataEventTime;
                dataEventTime = new OctetString(TimeofEvent);
                Variable EventTime = new Variable(oidEventTime, dataEventTime);
                objects.Add(EventTime);

                ObjectIdentifier TickStat = new ObjectIdentifier(ConfigurationManager.AppSettings["TicketStatus"]);
                ISnmpData TicketStat;
                TicketStat = new OctetString(TicketStatus);
                Variable TicketStatus1 = new Variable(TickStat, TicketStat);
                objects.Add(TicketStatus1);

                // (9) send a message now
                Messenger.SendTrapV2(0, VersionCode.V2, new IPEndPoint(address, port),
                    new OctetString("public"),
                    new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 8484, 1 }),
                    0,
                    objects);
            }
            catch (System.Exception ex)
            {
                File.AppendAllText(@"D:\Work Tasks\SNMPException.txt", ex.Message);
            }
        }

        private void Email(string attachments)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            //message.To.Add("camerons@nedbank.co.za");
            message.To.Add(ConfigurationSettings.AppSettings["EmailTo"]);
            message.Subject = "SNMP Error";


            message.Body = "Please stop and start the AptraSNMPMessenger service";
            System.Net.Mail.SmtpClient smtp = new   System.Net.Mail.SmtpClient("smtp.it.nednet.co.za", 25);
            smtp.UseDefaultCredentials = true;
            smtp.EnableSsl = false;
            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(attachments);
            message.Attachments.Add(attachment);
            smtp.Send(message);
        }

        protected override void OnStop()
        {// give the thread 3 seconds to stop
            _shutdownEvent.Set();
            if (!_thread.Join(3000))
            { 
                _thread.Abort();
            }
       }
    }
}

Background, get files dumped into a folder, read through those files and store in variables, send to another server. SNMP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.