Sunday, 1 March 2015

Kindly don’t use “Directory.Move” and ”File.Move” to handle files, always copy the files to another location and on success delete from the current location.

Directory.copy is not available as of now (.net 4.0) so use the below function.

public static void copyDirectory(string Src, string Dst)
        {
            String[] Files;
            if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
                Dst += Path.DirectorySeparatorChar;
            if (!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
            Files = Directory.GetFileSystemEntries(Src, "*.pdf");
            foreach (string Element in Files)
            {
                try
                {
                    File.Copy(Element, Dst + Path.GetFileName(Element), true);
                }
                catch (Exception)
                {
                    continue;
                }
            }
        }



                And it is better to handle delete in a separate function like.
      
        DeleteDirectory(Folderpath);

        protected void DeleteDirectory(string iDirectory)
        {
            try
            {
                Directory.Delete(iDirectory);
            }

            catch
            {

            }

        }


No comments:

Post a Comment