Filetype指令的作用及编程应用

374 篇文章 33 订阅 ¥29.90 ¥99.00

Filetype指令在编程中的作用是用于获取文件的类型或扩展名。它可以帮助开发者在处理文件时识别其类型,并根据需要采取相应的操作。在本文中,我们将详细探讨Filetype指令的使用方法,并提供一些示例代码来说明其在实际编程中的应用。

在很多编程语言中,都提供了一些内置函数或库来处理文件操作。其中,Filetype指令通常可以通过调用这些函数或库来实现。具体的语法和用法可能因编程语言而异,下面我们将以几种常见的编程语言为例进行说明。

  1. Python

在Python中,可以使用os模块的path模块来获取文件的扩展名。path模块提供了一个名为splitext的函数,它返回一个元组,其中包含文件名和文件的扩展名。以下是一个示例代码:

import os

filename = 'example.txt'
extension = os.path.splitext(filename)[1]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 根据文件类型获取文件图标 using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Runtime.InteropServices; using Microsoft.Win32; using System.Drawing; namespace FileTypeAndIcon { /// <summary> /// Structure that encapsulates basic information of icon embedded in a file. /// </summary> public struct EmbeddedIconInfo { public string FileName; public int IconIndex; } public class RegisteredFileType { #region APIs [DllImport("shell32.dll", EntryPoint = "ExtractIconA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] private static extern IntPtr ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex); [DllImport("shell32.dll", CharSet = CharSet.Auto)] private static extern uint ExtractIconEx(string szFileName, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons); [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)] private static unsafe extern int DestroyIcon(IntPtr hIcon); #endregion #region CORE METHODS /// <summary> /// Gets registered file types and their associated icon in the system. /// </summary> /// <returns>Returns a hash table which contains the file extension as keys, the icon file and param as values.</returns> public static Hashtable GetFileTypeAndIcon() { try { // Create a registry key object to represent the HKEY_CLASSES_ROOT registry section RegistryKey rkRoot = Registry.ClassesRoot; //Gets all sub keys' names. string[] keyNames = rkRoot.GetSubKeyNames(); Hashtable iconsInfo = new Hashtable(); //Find the file icon. foreach (string keyName in keyNames) { if (String.IsNullOrEmpty(keyName)) continue; int indexOfPoint = keyName.IndexOf("."); //If this key is not a file exttension(eg, .zip), skip it. if (indexOfPoint != 0) continue; RegistryKey rkFileType = rkRoot.OpenSubKey(keyName); if (rkFileType == null) continue; //Gets the default value of this key that contains the information of file type. object defaultValue = rkFileType.GetValue(""); if (defaultValue == null) continue; //Go to the key that specifies the default icon associates with this file type. string defaultIcon = defaultValue.ToString() + "\\DefaultIcon"; RegistryKey rkFileIcon = rkRoot.OpenSubKey(defaultIcon); if (rkFileIcon != null) { //Get the file contains the icon and the index of the icon in that file. object value = rkFileIcon.GetValue(""); if (value != null) { //Clear all unecessary " sign in the string to avoid error. string fileParam = value.ToString().Replace("\"", ""); iconsInfo.Add(keyName, fileParam); } rkFileIcon.Close(); } rkFileType.Close(); } rkRoot.Close(); return iconsInfo; } catch (Exception exc) { throw exc; } } /// <summary> /// Extract the icon from file. /// </summary> /// <param name="fileAndParam">The params string, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param> /// <returns>This method always returns the large size of the icon (may be 32x32 px).</returns> public static Icon ExtractIconFromFile(string fileAndParam) { try { EmbeddedIconInfo embeddedIcon = getEmbeddedIconInfo(fileAndParam); //Gets the handle of the icon. IntPtr lIcon = ExtractIcon(0, embeddedIcon.FileName, embeddedIcon.IconIndex); //Gets the real icon. return Icon.FromHandle(lIcon); } catch (Exception exc) { throw exc; } } /// <summary> /// Extract the icon from file. /// </summary> /// <param name="fileAndParam">The params string, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param> /// <param name="isLarge"> /// Determines the returned icon is a large (may be 32x32 px) /// or small icon (16x16 px).</param> public static Icon ExtractIconFromFile(string fileAndParam, bool isLarge) { unsafe { uint readIconCount = 0; IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero }; IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; try { EmbeddedIconInfo embeddedIcon = getEmbeddedIconInfo(fileAndParam); if (isLarge) readIconCount = ExtractIconEx(embeddedIcon.FileName, 0, hIconEx, hDummy, 1); else readIconCount = ExtractIconEx(embeddedIcon.FileName, 0, hDummy, hIconEx, 1); if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero) { // Get first icon. Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone(); return extractedIcon; } else // No icon read return null; } catch (Exception exc) { // Extract icon error. throw new ApplicationException("Could not extract icon", exc); } finally { // Release resources. foreach (IntPtr ptr in hIconEx) if (ptr != IntPtr.Zero) DestroyIcon(ptr); foreach (IntPtr ptr in hDummy) if (ptr != IntPtr.Zero) DestroyIcon(ptr); } } } #endregion #region UTILITY METHODS /// <summary> /// Parses the parameters string to the structure of EmbeddedIconInfo. /// </summary> /// <param name="fileAndParam">The params string, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param> /// <returns></returns> protected static EmbeddedIconInfo getEmbeddedIconInfo(string fileAndParam) { EmbeddedIconInfo embeddedIcon = new EmbeddedIconInfo(); if (String.IsNullOrEmpty(fileAndParam)) return embeddedIcon; //Use to store the file contains icon. string fileName = String.Empty; //The index of the icon in the file. int iconIndex = 0; string iconIndexString = String.Empty; int commaIndex = fileAndParam.IndexOf(","); //if fileAndParam is some thing likes that: "C:\\Program Files\\NetMeeting\\conf.exe,1". if (commaIndex > 0) { fileName = fileAndParam.Substring(0, commaIndex); iconIndexString = fileAndParam.Substring(commaIndex + 1); } else fileName = fileAndParam; if (!String.IsNullOrEmpty(iconIndexString)) { //Get the index of icon. iconIndex = int.Parse(iconIndexString); if (iconIndex < 0) iconIndex = 0; //To avoid the invalid index. } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return embeddedIcon; } #endregion } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值