locked
Problem reading files from network drive on startup RRS feed

  • Question

  • My program starts up when the machine starts up.  

    In a timer, I am reading from a folder using Directory.EnumerateFiles.    The folder is a network accessible drive that is not on the domain but the path is available without username password when the machine comes up.

    I'm getting errors saying the path does not exist as follows:  (Utils.Utils.TRC)

    01:47:00.974 PM UNC path is \\10.5.60.1\XMLGateway\BOOutBox
    01:47:01.067 PM Directory \\10.5.60.1\XMLGateway\BOOutBox Does not Exist.
    01:47:01.192 PM User unable to access \\10.5.60.1\XMLGateway\BOOutBox  Method failed with unexpected error code 1326.
    01:47:01.239 PM Error looking for files in \\10.5.60.1\XMLGateway\BOOutBox: The specified network name is no longer available.

    These errors persist indefinitely unless I happen to go into TaskManager.   When I do so, the errors clear up and the program begins to process files properly.  Likewise, if I stop my program and restart it, everything is fine.

    The end user wants to have this work from the startup so I need to understand some technique to get this program working.

    Here's the code called in the timer.

            public void FileTimerEvent(object source)
            {
                try
                {
                    if (Shutdown == true)
                    {
                        Utils.Utils.TRC("Shutting down..");
                        FileTimer.Change(Timeout.Infinite, Timeout.Infinite);
                        FileTimer.Dispose();
                        return;
                    }
                    FileTimer.Change(Timeout.Infinite, Timeout.Infinite);   //Turn off timer
                    //FileTimer.Stop();
                    Utils.Utils.TRC("File timer event looking for file in " + this.POSOutboxDir);

                    //string myError;
                    //bool DirReallyExists;
    //                if (DirectoryExists(this.POSOutboxDir, out myError, out DirReallyExists))
                    string ActualOutbox = MappedDriveResolver.GetUNCPath(this.POSOutboxDir);
                    string ActualDisposal = MappedDriveResolver.GetUNCPath(this.sDisposalLocation);
                    string ErrorLocation = MappedDriveResolver.GetUNCPath(this.errorLocation);
                    Utils.Utils.TRC("UNC path is " + ActualOutbox);
                    DirectoryInfo dI = new DirectoryInfo(ActualOutbox);
                    if (dI.Exists)
                    {
                        Utils.Utils.TRC("Directory " + ActualOutbox + " Exists.");
                    }
                    else
                    {
                        Utils.Utils.TRC("Directory " + ActualOutbox + " Does not Exist.");
                    }
                    try
                    {
                        Directory.GetAccessControl(ActualOutbox);
                        Utils.Utils.TRC("Directory " + ActualOutbox + " No access problem.");
                    }
                    catch (Exception Ex)
                    {
                        Utils.Utils.TRC("User unable to access " + ActualOutbox +"  " + Ex.Message);
                    }
    //                if(Directory.Exists(ActualOutbox))
    //                if (Directory.Exists(this.POSOutboxDir))
    //                {
                        if (ActualOutbox.Equals("\\") || ActualOutbox.Equals(""))
                        {
                            switch (NPos)
                            {
                                case POSConstants.PosConstants.GilbarcoPassport:
                                case POSConstants.PosConstants.RadiantXML:
                                    this.sLocalResults(this.POSOutboxDir  + " POSOutbox path empty or root. Change Outbox Path", false);
                                    Utils.Utils.TRC(this.POSOutboxDir + " POSOutbox path empty or root. Change Outbox Path");
                                    break;
                                case POSConstants.PosConstants.DresserNucleus:
                                    this.sLocalResults(this.POSOutboxDir + " POSOutbox path empty or root. Change Outbox Path", false);
                                    Utils.Utils.TRC(this.POSOutboxDir + " POSOutbox path empty or root. Change Outbox Path");
                                    break;
                                case POSConstants.PosConstants.VerifoneXML:
                                case POSConstants.PosConstants.VerifoneNAXML:
                                case POSConstants.PosConstants.VerifoneGemcom:
                                    this.sLocalResults(this.Pos + "Settings have not been saved for this POS. ", false);
                                    Utils.Utils.TRC(this.Pos + " POSOutbox path empty or root. Change Outbox Path");
                                    break;
                                default:
                                    this.sLocalResults(this.POSOutboxDir + " POSOutbox path empty or root. Change Outbox Path", false);
                                    Utils.Utils.TRC(this.POSOutboxDir + " POSOutbox path empty or root. Change Outbox Path");
                                    break;
                            }
                        }
                        ProcessExistingFiles(ActualOutbox,   //Where to get the files from
                                      this.Filter,        //Which files to pull from the above locations
                                      this.storeLocationID,    //The store
                                      this.POSStoreID,      //The POS Store
                                      this.Pos,          //The name of the POS
                                      this.NPos,         //The number of the POS
                                      this.localCustom,      //The routine to process the file
                                      ActualDisposal,   //Where to put the file
                                      ErrorLocation,
                                      this.bRenameExtension,
                                      this.sExtension,
                                       this.sLocalResults);        //Routine to notify the end user
                    this.LastProcessedTime = DateTime.Now;
                }
                catch (Exception e2)
                {
                    Utils.Utils.TRC("File Exception probably bad path:" + e2.Message + " processing file.");
                }
                finally
                {
                    Utils.Utils.TRC("File Timer Completed for " + this.POSOutboxDir);
                    //Reset Timer...
                    try
                    {
                        FileTimer.Change(POSConstants.PosConstants.LookForNewFilesInterval, POSConstants.PosConstants.LookForNewFilesInterval);
                    }
                    catch (Exception Ex)
                    {
                        Utils.Utils.TRC("Error Resetting Timer ignored: (Caused by bad file path..) "+ Ex.Message + "  "+  Ex.StackTrace);
                    }
                }
            }
    Tuesday, September 20, 2016 10:04 PM

Answers

  • Hi GPeele,

    Thank you for posting here.

    I want to know how do you achieve the DirectoryExists function. Based on my search. I cannot find the

    specified function on MSDN document.

    >>if I stop my program and restart it, everything is fine.

    First when you call timer. the program will fail. I would suggest you close your timer. and restart the timer.

    You also can create new thread to access the directory.

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Proposed as answer by DotNet Wang Wednesday, September 28, 2016 4:35 AM
    • Marked as answer by DotNet Wang Wednesday, September 28, 2016 4:39 AM
    Wednesday, September 21, 2016 3:20 AM