Exemple #1
0
        public static bool DeleteToRecycleBin(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            // remove end \\ char otherwise SHFileOperation() will failed
            if (path.EndsWith(@"\"))
            {
                path = path.Substring(0, path.Length - 1);
            }

            if (!File.Exists(path) && !Directory.Exists(path))
            {
                return(false);
            }

            try
            {
                _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
                pm.wFunc  = FO_DELETE;
                pm.pFrom  = path + '\0';
                pm.pTo    = null;
                pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI;
                return(SHFileOperation(pm) == 0);
            }
            catch (Exception ee)
            {
                Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message);
                Trace.WriteLine("### " + ee.StackTrace);
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int Delete(String path)
        {
            _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();

            pm.wFunc  = FO_DELETE;
            pm.pFrom  = path + '\0';
            pm.pTo    = null;
            pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
            return(SHFileOperation(pm));
        }
Exemple #3
0
 private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);
Exemple #4
0
 private static extern int SHFileOperation([System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] _SHFILEOPSTRUCT str);
Exemple #5
0
 public static int Delete(List<string> path)
 {
     _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
     pm.wFunc = FO_DELETE;
     pm.pFrom = path[0];
     for (int i = 1; i < path.Count; i++)
     {
         pm.pFrom += '\0' + path[i];
     }
     pm.pFrom += '\0';
     pm.pTo = null;
     pm.fFlags = FOF_ALLOWUNDO | FOF_WANTNUKEWARNING;
     return SHFileOperation(pm);
 }
        public static bool DeleteToRecycleBin(string path)
        {
            if (string.IsNullOrEmpty(path)) return false;

            // remove end \\ char otherwise SHFileOperation() will failed
            if (path.EndsWith(@"\"))
                path = path.Substring(0, path.Length - 1);

            if (!File.Exists(path) && !Directory.Exists(path))
                return false;

            try
            {
                _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
                pm.wFunc = FO_DELETE;
                pm.pFrom = path + '\0';
                pm.pTo = null;
                pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI;
                return (SHFileOperation(pm) == 0);
            }
            catch (Exception ee)
            {
                Trace.WriteLine("### [" + ee.Source + "] Exception: " + ee.Message);
                Trace.WriteLine("### " + ee.StackTrace);
                return false;
            }
        }