以下是一个基本的C#代码示例,用于使用Windows安装程序(.inf文件)安装设备驱动程序。该示例使用了Windows API函数来安装.inf文件。

using System.Runtime.InteropServices;

public class InfInstaller
{
    [DllImport("setupapi.dll", EntryPoint = "SetupCopyOEMInf")]
    private static extern bool SetupCopyOEMInf(
        string sourceInfFileName, string oemSourceMediaLocation,
        int OEMSourceMediaType, int copyStyle, string destinationInfFileName,
        int destinationInfFileNameSize, ref int requiredSize, string destinationInfFileNameComponent
    );

    public bool InstallInfFile(string infFilePath)
    {
        int size = 0;
        bool success = SetupCopyOEMInf(infFilePath, null, 1, 0, null, 0, ref size, null);
        if (!success)
        {
            int error = Marshal.GetLastWin32Error();
            if (error == 122)
            {
                char[] destInfPath = new char[size];
                success = SetupCopyOEMInf(infFilePath, null, 1, 0, new string(destInfPath), size, ref size, null);
            }
        }
        return success;
    }
}

您可以通过调用 InstallInfFile 方法并传递要安装的.inf文件的路径来使用此类。例如:

InfInstaller installer = new InfInstaller();
installer.InstallInfFile(@"C:\Drivers\DeviceDriver.inf");

请注意,此示例假定您拥有足够的权限来安装设备驱动程序。如果您需要管理员权限,请使用runas命令行来运行您的应用程序。