Error: The name server does not exist in current context

Error: The name server does not exist in current context

我正在使用 windows 应用程序发送带有附件的邮件。但是在这行代码中我得到错误

The name 'server' does not exist in current context

var directory = Server.MapPath("Attachment/");

我从 尝试过,但它对我不起作用

您正在尝试在 windows 应用程序中使用服务器对象,该服务器对象仅适用于 Web 应用程序。

您可以简单地指定目录的完整本地路径并使用它,这与在 Web 应用程序中必须基于虚拟目录进行映射不同。

如果 files/directory 的路径包含在与您的应用程序相同的文件夹中,您可以使用 Application.StartupPath 属性 并将其与您自己的值结合以获得完整路径.

var directory =  Application.StartupPath + "\Attachment\";