C# 如何打开一个文件的属性窗口,还有如何打开WebBrowser控件加载的网页的属性。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 如何打开一个文件的属性窗口,还有如何打开WebBrowser控件加载的网页的属性。相关的知识,希望对你有一定的参考价值。

参考技术A WebBrowser的网页属性对框很简单。
webBrowser
1.ShowPropertiesDialog();
就可以了。
文件的windows属性对话框比较麻烦,建议直接自己写窗体,读出各项属性。
如果要调用windows的,方法如下:
1、引用
命名空间
using
System.Runtime.InteropServices;
2、定义一个Struct,用于传递参数
[StructLayout(LayoutKind.Sequential)]
public
struct
SHELLEXECUTEINFO

public
int
cbSize;
public
uint
fMask;
public
IntPtr
hwnd;
[MarshalAs(UnmanagedType.LPStr)]
public
string
lpVerb;
[MarshalAs(UnmanagedType.LPStr)]
public
string
lpFile;
[MarshalAs(UnmanagedType.LPStr)]
public
string
lpParameters;
[MarshalAs(UnmanagedType.LPStr)]
public
string
lpDirectory;
public
int
nShow;
public
IntPtr
hInstApp;
public
IntPtr
lpIDList;
[MarshalAs(UnmanagedType.LPStr)]
public
string
lpClass;
public
IntPtr
hkeyClass;
public
uint
dwHotKey;
public
IntPtr
hIcon;
public
IntPtr
hProcess;

3、导入Windows
API函数
和一些常量
private
const
int
SW_SHOW
=
5;
private
const
uint
SEE_MASK_INVOKEIDLIST
=
12;
[DllImport("shell32.dll")]
static
extern
bool
ShellExecuteEx(ref
SHELLEXECUTEINFO
lpExecInfo);
4、写调用查看文件属性的对话框
public
static
void
ShowFileProperties(string
Filename)

SHELLEXECUTEINFO
info
=
new
SHELLEXECUTEINFO();
info.cbSize
=
System.Runtime.InteropServices.Marshal.SizeOf(info);
info.lpVerb
=
"properties";
info.lpFile
=
Filename;
info.nShow
=
SW_SHOW;
info.fMask
=
SEE_MASK_INVOKEIDLIST;
ShellExecuteEx(ref
info);

这样就能通过ShowFileProperties("xxxx")来调用查看文件对话框了。

如何单击按钮并打开窗口以在c#中发送电子邮件

【中文标题】如何单击按钮并打开窗口以在c#中发送电子邮件【英文标题】:How to click button and open window to send email in c# 【发布时间】:2015-02-25 12:47:30 【问题描述】:

我想创建一个按钮,以便用户可以单击,它将打开一个新的小窗口,他们可以在其中发送电子邮件。此窗口将具有“发件人”、“收件人”、“主题”、“内容”字段,所有这些字段都将具有默认文本,用户可以对其进行编辑(“发件人”字段除外)。见下图:

我尝试了什么:

我通过以下方式创建了一个电子邮件表单:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<p>
From:<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</p>
To:<asp:Literal ID="Literal2" runat="server"></asp:Literal>
<p>
Subject:<asp:Literal ID="Literal3" runat="server"></asp:Literal>
</p>
Content:<asp:Literal ID="Literal4" runat="server"></asp:Literal>
</form>
</body>
</html>

然后我尝试将此表单与我当前的代码链接: 当前代码:我可以通过此代码发送电子邮件:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtpclientaddresshere");
mail.From = new MailAddress("defaultFromEmail@domain.com");
mail.To.Add("email1@yahoo.com,email2@yahoo.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail";
SmtpServer.Credentials = new System.Net.NetworkCredential("mysmtpserver@something.com", "");
SmtpServer.Send(mail);

现在我不知道是否有权将上述表格用于电子邮件窗口目的?我如何格式化所有字段并将其链接到我的工作代码?

【问题讨论】:

好吧,我就是这样做的,只是我将加密的密码存储在 web.config 中。虽然这应该在 Codereview 上,但我正在等待一些好的替代方案 如何链接每个表单中的字段? 【参考方案1】:

我的做法是这样的:

public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
    
        try
        
            SmtpClient _smtp = new SmtpClient();

            MailMessage _message = new MailMessage();

            _message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name

            _message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited

            _message.Subject = _subject; // The subject of the email
            _message.Body = _body; // The body of the email

            _smtp.Port = 587; // Google mail port
            _smtp.Host = "smtp.gmail.com"; // Google mail address
            _smtp.EnableSsl = true;
            _smtp.UseDefaultCredentials = false;
            _smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password

            _smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtp.Send(_message);

            ShowMessageBox("Your message has been successfully sent.", "Success", 2);
        

        catch (Exception ex)
        
            ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
        
    

我是这样使用它的:

SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");

图片如下:

(虽然我使用的是 WinForms 而不是 Windows Presentation Forms)。

希望这个答案对你有所帮助。

干杯!

【讨论】:

能分享一下WinForms的代码(图片)吗? 我正在使用 .net 3.5 如果我更改为不同的版本,我的项目会遇到一些问题 对于我的答案图像上显示的一堆文本框,您只需拖放即可。 .Net 在这种情况下无关紧要,如果您已经在使用 .Net 3.5,请继续使用,无需更改 .Net 版本。如果您不知道怎么做,请在 youtube 上找到教程,有很多关于如何在 Windows 窗体上拖放文本框的教程。注意:如果您发现这是您要找的,请接受我的回答。谢谢 是的,一旦我可以在我身边实施它,我肯定会接受它作为答案。我真的相信这取决于 .net 版本,因为我没有要添加的任何 Windows 窗体模板。可能是项目框架依赖于.net框架。我不知道如何添加 Windows 窗体。我确实尝试过这个修复,但它没有帮助(chiragrdarji.wordpress.com/2008/05/06/…)。如果有该模板,我将按照本教程添加表单:youtube.com/watch?v=Y9L85OBo1c4 不确定我是否真的可以帮助你,因为我安装了 Visual Studio 2010(仅用于制作安装程序)和 2013,这两个软件都已经有了 windows 窗体。你的这些问题从来没有发生在我身上。

以上是关于C# 如何打开一个文件的属性窗口,还有如何打开WebBrowser控件加载的网页的属性。的主要内容,如果未能解决你的问题,请参考以下文章

C# WPF如何关闭通过父窗口打开的所有子窗口

C# WinForm中,如何判断窗口已打开

C#如何关闭一个窗口的同时打开另一个窗口

C# 主窗口调用子窗口的值传递?

C#应用程序如何不弹出cmd命令行窗口执行

c#中,如何实现一个按钮控制另一个窗口的打开和关闭,即点击一下,新窗口打开,再点击一下,打开的新窗