易截截图软件、单文件、免安装、纯绿色、仅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()
{
    /* 定义一个整数类型的变量,用来存放后面算式的值 */
    int logic;    
 
    int a = 1;
    int b = 2;
    int c = 3;
 
&n ......

C_使用二维数组作出矩阵图

 源码:
# include <stdio.h>
 
int main()
{
    int array[16][16];
    int i, j, k, m, n;
     
    /* 变量初始化 */
    m = 1;
    while(m == 1)
    {
   ......

C_定义字符型二维数组实现简单的编辑器

 源码:
# include <stdio.h>
/* 宏定义 */
# define MAX 100
# define LEN 80
 
/* 一个非常简单的文本编辑器 */
int main()
{
    char text[MAX][LEN];  // 定义字符型数组
    register int t, i, j;    /* 定义三个寄存器变量 */
  ......

C_综合使用数组实现简单的学生成绩管理系统

 源码:
/* 学生成绩查询系统 */
# include <stdio.h>
# include <stdlib.h>
 
int main( )
{
    int select;
    int i, j;
    int score[5][7];
    int average = 0;
    int sum = 0;
  &n ......

C_利用函数的引用调用实现两数的交换

 源码:
# include <stdio.h>
 
void swap(int *x, int *y);
 
int main()
{
    int i, j;
 
    i = 12;
    j = 36;
 
    printf("i and j before swapping: %d %d\n", i, j);
 
 &nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号