2017-06-02 13 views
8

Tôi đã cố sử dụng firewallAPI.dll để thêm quy tắc. Nó hoạt động tốt cho calc.exe (hoặc một số file khác) như dưới đây mô tả nhưng không cho msdtc.exe với ngoại lệ sau:Thêm quy tắc tường lửa cho Điều phối viên giao dịch phân tán (msdtc.exe)

System.IO.FileNotFoundException: 'Hệ thống không thể tìm thấy file quy định. (Ngoại lệ từ HRESULT: 0x80070002)'

Ví dụ:

static void Main(string[] args) 
{ 
    var manager = GetFirewallManager(); 
    if (manager.LocalPolicy.CurrentProfile.FirewallEnabled) 
    { 
     var path = @"C:\Windows\System32\calc.exe"; 
     //var path = @"C:\Windows\System32\msdtc.exe"; // System.IO.FileNotFoundException: 'The system cannot find the file specified. 
     AuthorizeApplication("Test", path, NET_FW_SCOPE_.NET_FW_SCOPE_ALL, NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY); 
    } 
} 

private const string CLSID_FIREWALL_MANAGER = 
    "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; 

private static NetFwTypeLib.INetFwMgr GetFirewallManager() 
{ 
    Type objectType = Type.GetTypeFromCLSID(
     new Guid(CLSID_FIREWALL_MANAGER)); 
    return Activator.CreateInstance(objectType) 
     as NetFwTypeLib.INetFwMgr; 
} 

private const string PROGID_AUTHORIZED_APPLICATION = 
    "HNetCfg.FwAuthorizedApplication"; 
public static bool AuthorizeApplication(string title, string applicationPath, 
    NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) 
{ 
    // Create the type from prog id 
    Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); 
    INetFwAuthorizedApplication auth = Activator.CreateInstance(type) 
     as INetFwAuthorizedApplication; 
    auth.Name = title; 
    auth.ProcessImageFileName = applicationPath; 
    auth.Scope = scope; 
    auth.IpVersion = ipVersion; 
    auth.Enabled = true; 

    INetFwMgr manager = GetFirewallManager(); 
    manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); 
    return true; 
} 

Lưu ý: Tôi đã kiểm tra các thư mục và xem các tập tin nằm đúng ... Ai có thể giúp bổ sung quy tắc tường lửa cho Distributed Điều phối viên giao dịch? Có lẽ tôi nên cố gắng để thêm một tập tin vào tường lửa (không msdtc.exe)?

Trả lời

8

Dự án> Thuộc tính> tab Xây dựng, bỏ chọn hộp kiểm "Ưu tiên 32 bit". Bạn không thích nó, không có phiên bản 32-bit của msdtc.exe.

Tại sao trình chuyển hướng hệ thống tệp gây ra FileNotFoundException được giải thích rõ trong this MSDN article.

Các vấn đề liên quan