如何让UserControl添加附件

How to make UserControl add attachments

如何在 Windows 应用程序 c#

中创建用户控件

我需要在表单中制作附件文件,但我需要在单击时使用用户控件 按钮浏览并选择文件或图像在表单中添加用户控件?

在表单上添加一个按钮并使用OpenFileDialog,像这样:

private void buttonGetFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "Text files | *.txt"; // file types, that will be allowed to upload
    dialog.Multiselect = false; // allow/deny user to upload more than one file at a time
    if (dialog.ShowDialog() == DialogResult.OK) // if user clicked OK
    {
        String path = dialog.FileName; // get name of file
        using (StreamReader reader = new StreamReader(new FileStream(path, FileMode.Open), new UTF8Encoding())) // do anything you want, e.g. read it
        {
            // ...
        }
    }
}