0

I'm working on a project for windows mobile 6.5. I am using C # with compact framework 3.5 (CF 3.5) and SDK for Windows Mobile 6.5.

My routine writes files to a temporary directory for further processing. After a few days the file is renamed and directed to a purge.

When trying to delete the file the following error occurs: Access to the path '\Application Data\Volatile\Temp\20170822-97703.Nf.env' is denied.

Where:

  • \Application Data\Volatile is the default temporary directory Path.GetTempPath()
  • \Temp is my temporary directory
  • 20170822-97703.Nf.env is my file.

Code:

const string dirTemp= "Temp";

public void PurgeFiles()
        {
            DateTime datePurge= new DateTime();
            datePurge= DateTime.Now.AddDays(-7);
            var files= FindFiles();

            foreach (string file in files)
            {
                var dateAlt = Directory.GetLastWriteTime(file);
                if (dateAlt< datePurge)
                {                    
                    Directory.Delete(file);
                }
            }
        }

private string[] FindFiles()
        {
            string searchPattern;
            string dirLocal;

            dirLocal= Path.GetTempPath();
            dirLocal= Path.Combine(dirLocal, dirTemp);

            if (Directory.Exists(dirLocal))
            {
                searchPattern = "*.Env";
                var files = Directory.GetFiles(dirLocal, searchPattern);
                return files;
            }
            else
                return new string[0];
        }

Save file

public bool SaveFile(string dir, string fileName, string content)
        {
            try
            {                
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                string pathFile = Path.Combine(dir, fileName);

                if (File.Exists(pathFile))
                    return true; 

                //Salva os dados
                StreamWriter fileConf = new StreamWriter(pathFile);
                fileConf.Write(content);
                fileConf.Flush();
                fileConf.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

Mark the file as processed

public void MarkFile(string fileName)
        {
            try
            {
                string newFileName= fileName + ".env";

                if (File.Exists(newFileName))
                    return; 

                File.Move(fileName , newFileName);
            }                
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
  • Perhaps the file is locked and will be delete-able after a restart. If you add a try/catch around File.Delete then you can try again next time. – stuartd Aug 30 '17 at 20:39
  • We need to see the code that creates and writes the temp file. It's most likely you're not releasing the file handle at that point. – ctacke Aug 30 '17 at 21:22
  • Add the save and rename functions. Quando salvo o arquivo eu fecho o mesmo, mas quando renomeio o arquivo não. Isto é necessário? – Alexandre Cavaloti Aug 30 '17 at 21:45
  • 1
    Check if you can delete the file manually. Possibly you have a special char in the name. – josef Sep 01 '17 at 05:01

1 Answers1

0

To can delete the file I changed the name of file and the line below

Directory.Delete(file);

for

File.Delete(file);