易截截图软件、单文件、免安装、纯绿色、仅160KB

C Sharp(C#)中如何删除文件(文件夹)

C Sharp(C#)中如何删除文件(文件夹)
直接删除:
using System.IO;
...
string filePath = @"D:\...\xxx.xxx";

if (File.Exists(filePath))
{
File.Delete(filePath);
}
else
{
Console.WriteLine("file not exist.");
Console.ReadLine();
}
删除到回收站:
using System.Runtime.InteropServices;
namespace CSharp
{
class Program
{
private const int FO_DELETE = 3;
private const int FOF_ALLOWUNDO = 0x40;
private const int FOF_NOCONFIRMATION = 0x0010;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public int wFunc;
public string pfrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
static void Main(string[] args)
{
string filePath = @"D:\...\xxx.xxx";

if (File.Exists(filePath))
{
SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
fileop.wFunc = FO_DELETE;
fileop.pfrom = filePath + '\0' + '\0';
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
SHFileOperation(ref fileop);
Console.WriteLine("delete ok");
Console.ReadLine();
}
else
{
Console.WriteLine("file not exist.");
Console.ReadLine();
}
}
}
}


相关文档:

C_普通位运算

 源码:
# include <stdio.h>
 
int main()
{
    /* 定义了一个无符号字符型变量,此变量只能用来存储无符号数 */
    unsigned char result;
    
    int a, b, c, d;
    a = 2;
    b = 4; ......

C_判断语句if与else的组合使用

 源码:
# include <stdio.h>
 
int main()
{
    int x, y;
    printf("请输入自变量x:");
    scanf("%d", &x);
 
    if(x < 6)
    {
        ......

C_使用for循环画菱形

 源码:
# include <stdio.h>
 
int main()
{
    int i, j, k;
     
    /* 变量i从0到4,表示所画菱形图的第一至第五行 */
    for(i = 0; i <= 4; i++)
    {
        /* ......

C_各种数组的初始化实例

 源码:
# include <stdio.h>
 
int main()
{
    /* 有尺寸 */
    /* 一维整形数组初始化 */
    int  array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 
    /* 一维字符型数组初始化,最后一个元素自动添加为‘/0 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号