【问题标题】:Converting VBS code to C#将 VBS 代码转换为 C#
【发布时间】:2011-11-17 04:32:17
【问题描述】:

我只有下面的代码,它作为 hMailServer 的 DCOM API 在http://www.hmailserver.com/documentation/latest/?page=com_example_account_create 提供。下面的脚本工作正常。它没有任何参考。安装 hMailServer 后,运行以下代码可以创建一个帐户。现在,我在 C# 中需要同样的东西。他们没有为我提供任何 C# 库,我用谷歌搜索了它,但没有相关结果我只有下面的代码,但根据 hMailServer API,他们说你可以将下面的脚本转换为你想要的任何语言。但是怎么做?我什至无法理解如何开始写第一行。任何人请帮助我。

Dim obApp
   Set obApp = CreateObject("hMailServer.Application")

   ' Authenticate. Without doing this, we won't have permission
   ' to change any server settings or add any objects to the
   ' installation.   
   Call obApp.Authenticate("Administrator", "your-main-hmailserver-password")

   ' Locate the domain we want to add the account to
   Dim obDomain
   Set obDomain = obApp.Domains.ItemByName("example.com")

   Dim obAccount
   Set obAccount = obDomain.Accounts.Add

   ' Set the account properties
   obAccount.Address = "account@example.com"
   obAccount.Password = "secret"
   obAccount.Active = True
   obAccount.MaxSize = 100 ' Allow max 100 megabytes

   obAccount.Save

【问题讨论】:

  • 只是为了确定:这是 vbscript (asp-classic) 还是 vb.net?
  • 没关系:vb.net 不再真正使用 Call,它几乎可以肯定是经典的 asp。
  • 正如您在链接网站上看到的那样,它只是 vbs。没有连接到asp。
  • 不幸的是vb.net仍然允许你使用Call、CreateObject和On Error Resume Next等。
  • @Stingy:但没有什么能强迫你使用它们。

标签: c# com vbscript


【解决方案1】:

将 COM 对象 (hMailServer) 作为参考添加到您的 C# 项目中,并将其余代码转换为 C#。

它看起来像这样:

var app = new hMailServer.Application();

// Authenticate. Without doing this, we won't have permission
// to change any server settings or add any objects to the
// installation.   
app.Authenticate("Administrator", "your-main-hmailserver-password");

// Locate the domain we want to add the account to
var domain = app.Domains["example.com"];

var account = domain.Accounts.Add();

// Set the account properties
account.Address = "account@example.com";
account.Password = "secret";
account.Active = true;
account.MaxSize = 100; // Allow max 100 megabytes

account.Save();

【讨论】:

  • 我找不到任何此类参考文件。可以给我下载链接吗?
  • 无需下载任何东西。只需添加已经存在的 COM 对象作为参考。有关如何添加对 C# 项目的引用的更多信息,请参阅 here。尤其是“在 Visual C# 中添加引用”主题。
  • 好吧。我收到以下错误错误Non-invocable member 'hMailServer.IInterfaceDomains.ItemByName' cannot be used like a method.
  • 因为我不打算安装那个 COM 对象,你需要自己弄清楚。也许ItemByName 是一个索引器? app.Domains["example.com"];。如果没有,请双击对 COM 对象的引用以在对象浏览器中打开它。在那里你可以看到ItemByName 到底是什么。
  • 自 C# 3.0 起支持 var。但是,您可以明确指定类型,只需将它们更改为行所需的类型。第一行:hMailServer.Application app = new hMailServer.Application();。我不知道的其他行,您需要将var 更改为右侧表达式的类型,即app.Domains["example.com"]domain.Accounts.Add() 返回的内容。可能:hMailServer.DomainhMailServer.Account
【解决方案2】:

我希望这仍然相关并且可以帮助某人。这里我只是简单的使用get item by name属性来拉取hMailServer中配置的域名。

protected void Page_Load(object sender, EventArgs e)
{
    var app = new hMailServer.Application();

    // Authenticate. Without doing this, we won't have permission
    // to change any server settings or add any objects to the
    // installation.   
    app.Authenticate("Administrator", "your.admin.password.here");

    // Locate the domain we want to add the account to
    var domain = app.Domains.get_ItemByName("your.configured.domain.name.here");

    var account = domain.Accounts.Add();

    // Set the account properties
    account.Address = "account.name.here";
    account.Password = "pass.word.here";
    account.Active = true;
    account.MaxSize = 100; // Allow max 100 megabytes

    account.Save();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多