Esempio n. 1
0
        internal static void CreateOrClearTempPath(string path)
        {
            #region Safety check

            // Make sure we never delete any paths that are not safely tucked in our temp folder
            string baseTemp = _baseTemp.TrimEnd(CA_BS_FS_Space);

            // @DIRSEP: getting rid of this concat is more trouble than it's worth
            // This method is called rarely and only once in a row
            bool pathIsInTempDir = path.PathStartsWithI(baseTemp + "\\");

            Misc.AssertR(pathIsInTempDir, "Path '" + path + "' is not in temp dir '" + baseTemp + "'");

            if (!pathIsInTempDir)
            {
                return;
            }

            #endregion

            if (Directory.Exists(path))
            {
                try
                {
                    foreach (string f in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
                    {
                        new FileInfo(f).IsReadOnly = false;
                    }

                    foreach (string d in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
                    {
                        Misc.Dir_UnSetReadOnly(d);
                    }
                }
                catch (Exception ex)
                {
                    Log("Exception setting temp path subtree to all non-readonly.\r\n" +
                        "path was: " + path, ex);
                }

                try
                {
                    foreach (string f in FastIO.GetFilesTopOnly(path, "*"))
                    {
                        File.Delete(f);
                    }
                    foreach (string d in FastIO.GetDirsTopOnly(path, "*"))
                    {
                        Directory.Delete(d, recursive: true);
                    }
                }
                catch (Exception ex)
                {
                    Log("Exception clearing temp path " + path, ex);
                }
            }
            else
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception ex)
                {
                    Log("Exception creating temp path " + path, ex);
                }
            }
        }