Search code examples
c#compact-frameworkwindows-mobile-6.5.net-cf-3.5

Error on delete file at Windows Mobile 6.5


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);
            }
        }

Solution

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

    Directory.Delete(file);

    for

    File.Delete(file);