示例#1
0
        /// <summary>
        ///     Deletes the folders / files.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="pop"></param>
        public static void DeleteFilesOrFolders(string path, bool pop = true)
        {
            var del  = false;
            var attr = new FileAttributes();

            if (!path.IsNullOrEmpty())
            {
                if (IsDirectory(path) || IsFile(path))
                {
                    attr = File.GetAttributes(path);
                }
            }
            //detect whether its a directory or file
            if (attr.IsEmpty())
            {
                return;
            }
            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
            {
                var directory = new DirectoryInfo(path);
                try {
                    foreach (var file in directory.GetFiles())
                    {
                        DeleteFile(file.ToString());
                    }
                    foreach (var subDirectory in directory.GetDirectories())
                    {
                        subDirectory.Delete(true);
                    }
                    directory.EmptyThisDirectory();
                    directory.Delete(true);
                }
                catch (Exception g) {
                    MsgBx.Msg(g.ToString(), "Deletion Error");
                }
            }
            else
            {
                try {
                    if (!path.FileExists())
                    {
                        return;
                    }
                    del = DeleteFile(path);
                }
                catch (Exception d) {
                    MsgBx.Msg(d.ToString(), "Deletion Error");
                }
            }

            if (!pop)
            {
                return;
            }
            if (del)
            {
                MsgBx.Msg("File(s) have been deleted", "File Deletion");
            }
        }