如何使UserControl添加附件

时间:2015-07-29 06:52:58

标签: c# c#-4.0

如何在Windows应用程序中进行用户控制c#

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

1 个答案:

答案 0 :(得分:1)

在表单上添加一个按钮并使用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
        {
            // ...
        }
    }
}