Csharp实现回收站清空

调用shell32.dll 实现回收站清空。

首先用DllImport将函数从shell32.dll中导入。 

        [DllImport( " shell32.dll " )]
        
static   extern   int  SHQueryRecycleBin( string  pszRootPath,  ref  SHQUERYRBINFO pSHQueryRBInfo);
        [DllImport(
" shell32.dll " )]
        
static   extern   int  SHEmptyRecycleBin(IntPtr hWnd,  string  pszRootPath, uint  dwFlags);
        [DllImport(
" shell32.dll " )]
        
static   extern   int  SHUpdateRecycleBinIcon();

SHQueryRecycleBin是回收站信息查询函数;SHEmptyRecycleBin是回收站清空函数;SHUpdateRecycleBinIcon是回收站图标还原函数。然后还得定义SHQUERYRBINFO结构,是用来保存回收站信息的,还有几个常量,是用于SHEmptyRecycleBin函数的最后一个参数的,

 

        [StructLayout(LayoutKind.Explicit, Size = 20 )]
            
public   struct  SHQUERYRBINFO
        
...

        
//      No dialog box confirming the deletion of the objects will be displayed.
         const   int  SHERB_NOCONFIRMATION  =   0x00000001 ;
        
//      No dialog box indicating the progress will be displayed. 
         const   int  SHERB_NOPROGRESSUI  =   0x00000002 ;
        
//      No sound will be played when the operation is complete. 
         const   int  SHERB_NOSOUND  =   0x00000004 ;

然后就可以调用这几个函数进行回收站清空了,

 

         /// <summary>
        
/// 查询回收站信息
        
/// </summary>
        
/// <param name="rootPath">要查询的回收站文件夹路径,当为空时查询回收站</param>
        
/// <param name="sqrbi">存储回收站信息</param>
        
/// <returns>返回零表示操作成功</returns>

         public   static   int  QueryRecycleBin( string  rootPath, ref  SHQUERYRBINFO sqrbi)
        
{
            
return SHQueryRecycleBin(rootPath,ref sqrbi);
        }

        
/// <summary>
        
/// 清空回收站
        
/// </summary>
        
/// <param name="rootPath">要清空的回收站文件夹的路径,当为空时清空回收站</param>
        
/// <returns>返回零表示操作成功</returns>

         public   static   int  EmptyRecycleBin( string  rootPath)
        
{
            
return SHEmptyRecycleBin(IntPtr.Zero, rootPath, 
                SHERB_NOCONFIRMATION 
| SHERB_NOPROGRESSUI | SHERB_NOSOUND);
        }

        
/// <summary>
        
/// 还原回收站图标
        
/// </summary>
        
/// <returns></returns>

         public   static   int  UpdateRecycleBinIcon()
        
{
            
return SHUpdateRecycleBinIcon();
        }

在程序了,首先调用QueryRecycleBin函数查询,如果回收站的字节数或者项目数不为零,就调用EmptyRecycleBin函数进行清空,如果清空的过程中发生错误,调用UpdateRecycleBinIcon函数将回收站的图标还原。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于csharp实现socket网络编程,可以使用System.Net.Sockets命名间提供的类和方法来实现。下面是一个简单的示例代码: ``` using System; using System.Net; using System.Net.Sockets; using System.Text; class Program { static void Main(string[] args) { // 创建一个新的Socket Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 设置服务端IP地址和端口号 IPAddress ipAddress = IPAddress.Parse("192.168.0.100"); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 8888); try { // 绑定IP地址和端口号 serverSocket.Bind(ipEndPoint); // 开始监听客户端连接 serverSocket.Listen(10); Console.WriteLine("等待客户端连接..."); // 接收客户端连接,返回一个新的Socket Socket clientSocket = serverSocket.Accept(); Console.WriteLine("客户端已连接"); // 向客户端发送数据 byte[] bytes = Encoding.UTF8.GetBytes("欢迎使用Socket网络编程"); clientSocket.Send(bytes); // 接收来自客户端的数据 while (true) { bytes = new byte[1024]; int length = clientSocket.Receive(bytes); if (length > 0) { string message = Encoding.UTF8.GetString(bytes, 0, length); Console.WriteLine("接收到来自客户端的消息:" + message); // 如果客户端发送“exit”退出指令,则关闭连接 if (message.Equals("exit")) { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); break; } } } } catch (Exception e) { Console.WriteLine(e.Message); } } } ``` 以上就是一个简单的csharp实现socket网络编程的示例代码,可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值