C# 注册自己的右键菜单 (文件夹/文件)

// 添加到注册表private void btnRegister_Click(object sender, EventArgs e){ if (this.tbMenuTitle.Text.Length == 0) return; // 注册到文件 if (this.ckRegToFile.Checked) { RegistryKey shell = Registry.ClassesRoot.OpenSubKey("*", true).OpenSubKey("shell", true); if (shell == null) shell = Registry.ClassesRoot.OpenSubKey("*", true).CreateSubKey("shell"); RegistryKey custome = shell.CreateSubKey(this.tbMenuTitle.Text); RegistryKey cmd = custome.CreateSubKey("command"); cmd.SetValue("", Application.ExecutablePath + " %1"); cmd.Close(); custome.Close(); shell.Close(); } // 注册到文件夹 if (this.ckRegToDir.Checked) { RegistryKey shell = Registry.ClassesRoot.OpenSubKey("directory", true).OpenSubKey("shell", true); if (shell == null) shell = Registry.ClassesRoot.OpenSubKey("directory", true).CreateSubKey("shell"); RegistryKey custome = shell.CreateSubKey(this.tbMenuTitle.Text); RegistryKey cmd = custome.CreateSubKey("command"); cmd.SetValue("", Application.ExecutablePath + " %1"); cmd.Close(); custome.Close(); shell.Close(); } MessageBox.Show("注册成功!", "提示");}// 反注册private void btnUnRegister_Click(object sender, EventArgs e){ RegistryKey shell = Registry.ClassesRoot.OpenSubKey("*", true).OpenSubKey("shell", true); if (shell != null) shell.DeleteSubKeyTree(this.tbMenuTitle.Text); shell = Registry.ClassesRoot.OpenSubKey("directory", true).OpenSubKey("shell", true); if (shell != null) shell.DeleteSubKeyTree(this.tbMenuTitle.Text); shell.Close(); MessageBox.Show("反注册成功!", "提示");}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,你可以使用`System.IO`和`Microsoft.Office.Interop.Excel`命名空间来实现定时读取指定文件夹中的所有Excel数据。以下是一个示例代码: ```csharp using System; using System.IO; using Excel = Microsoft.Office.Interop.Excel; namespace ExcelDataReader { class Program { static void Main(string[] args) { // 定义要读取的文件夹路径 string folderPath = @"C:\path\to\folder"; // 创建Excel应用程序对象 Excel.Application excelApp = new Excel.Application(); // 获取文件夹中的所有文件 string[] files = Directory.GetFiles(folderPath); // 遍历文件夹中的每个文件 foreach (string file in files) { // 判断文件是否为Excel文件 if (Path.GetExtension(file).Equals(".xlsx", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(file).Equals(".xls", StringComparison.OrdinalIgnoreCase)) { // 打开Excel文件 Excel.Workbook workbook = excelApp.Workbooks.Open(file); // 获取第一个工作表 Excel.Worksheet worksheet = workbook.Sheets[1]; // 获取工作表中的数据 Excel.Range range = worksheet.UsedRange; object[,] data = range.Value; // 处理数据(这里只是打印数据) int rowCount = data.GetLength(0); int colCount = data.GetLength(1); for (int row = 1; row <= rowCount; row++) { for (int col = 1; col <= colCount; col++) { Console.Write(data[row, col] + "\t"); } Console.WriteLine(); } // 关闭Excel文件 workbook.Close(); } } // 退出Excel应用程序 excelApp.Quit(); } } } ``` 在上述代码中,我们首先定义了要读取的文件夹路径 `folderPath`。然后,通过创建 `Excel.Application` 对象来打开Excel应用程序。接下来,使用 `Directory.GetFiles()` 方法获取文件夹中的所有文件,并遍历每个文件。在遍历过程中,我们使用 `Path.GetExtension()` 方法来判断文件是否为Excel文件。如果是Excel文件,我们打开该文件并获取第一个工作表的数据。最后,我们可以对数据进行处理(这里只是简单地打印数据),然后关闭Excel文件并退出Excel应用程序。 请注意,为了使用 `Microsoft.Office.Interop.Excel` 命名空间,你需要在项目中添加对 `Microsoft.Office.Interop.Excel` 的引用。你可以在Visual Studio中右键单击项目,在“引用”下选择“添加引用”,然后在“COM”选项卡下找到并选择 `Microsoft Excel xx.x Object Library`。 此外,你还需要安装Excel Interop组件。可以在Visual Studio的“工具”菜单下选择“NuGet包管理器”->“程序包管理器控制台”,然后执行以下命令进行安装: ``` Install-Package Microsoft.Office.Interop.Excel ``` 希望这可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值