要实现在C# Winform中上传的每一个文件名横向显示到界面上,并可以进行查看和删除,可以按照以下步骤进行操作:

  1. 在Winform界面上添加一个ListBox控件,用于显示上传的文件名。
  2. 在界面上添加一个Button控件,用于上传文件。
  3. 在Button的Click事件处理程序中,使用OpenFileDialog选择要上传的文件,并将文件名添加到ListBox控件中。
  4. 在ListBox的SelectedIndexChanged事件处理程序中,根据选中的文件名显示对应的文件内容。
  5. 在界面上添加一个Button控件,用于删除选中的文件。
  6. 在Button的Click事件处理程序中,根据选中的文件名从ListBox中移除,并删除对应的文件。

以下是一个实现的示例代码:

using System;
using System.IO;
using System.Windows.Forms;

namespace FileUploadExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string fileName in openFileDialog.FileNames)
                {
                    listBoxFiles.Items.Add(fileName);
                }
            }
        }

        private void listBoxFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxFiles.SelectedItem != null)
            {
                string selectedFileName = listBoxFiles.SelectedItem.ToString();
                string fileContent = File.ReadAllText(selectedFileName);
                MessageBox.Show(fileContent, "File Content");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listBoxFiles.SelectedItem != null)
            {
                string selectedFileName = listBoxFiles.SelectedItem.ToString();
                listBoxFiles.Items.Remove(selectedFileName);
                File.Delete(selectedFileName);
            }
        }
    }
}

在上述示例代码中,通过btnUpload按钮的Click事件处理程序选择要上传的文件,并将文件名添加到listBoxFiles控件中。通过listBoxFiles的SelectedIndexChanged事件处理程序,可以查看选中文件的内容。通过btnDelete按钮的Click事件处理程序,可以删除选中的文件,并从listBoxFiles中移除对应的文件名。

请注意,上述示例代码中仅仅实现了文件名的横向显示、查看和删除,并未实现文件上传功能。如果需要实现文件上传功能,可以使用第三方库或自行编写文件上传逻辑

C# winform中上传的每一个文件名横向显示到界面上并可进行查看和删除

原文地址: https://www.cveoy.top/t/topic/h8r2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录