简体   繁体   English

我正在尝试创建一个上传文件按钮,但每当我尝试使用文件时,我都会收到错误“空路径名称不合法”

[英]I am trying to create an upload file button, but whenever I try and use a file I get the error “Empty path name is not legal”

Every time I click on a file, I get the error 每次我点击一个文件,我都会收到错误

Empty path name is not legal 空路径名称不合法

I need a user to upload a .txt or .csv file and have the contents of that file to display in the data grid view. 我需要用户上传.txt.csv文件,并让该文件的内容显示在数据网格视图中。 Everything works fine except for this one error that occurs on this line: 一切正常,除了这一行发生的这一个错误:

var sr = new StreamReader(openFileDialog1.FileName);

Full code. 完整代码。

private void selectButton_Click (object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            var sr = new StreamReader(openFileDialog1.FileName);
            SetText(sr.ReadToEnd());
        }
        catch (SecurityException ex)
        {
            MessageBox.Show($"Security error.\n\nError message{ex.Message}\n\n" +
                $"Details:\n\n{ex.StackTrace}");
        }
    }
}

尝试在openFileDialog1上添加过滤器之类的。

openFileDialog1.Filter = "text file(*.txt)|*.txt|csv file(*.csv)|*.csv";

Check the FileName before open file: 在打开文件之前检查FileName:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        if (string.IsNullOrEmpty(openFileDialog1.FileName) {
            // ..
            return; 
        }

        try
        {
            var fileStream = openFileDialog1.OpenFile();

            using (StreamReader reader = new StreamReader(fileStream))
            {
                SetText(sr.ReadToEnd());
            }       
        }
        catch (SecurityException ex)
        {
            MessageBox.Show($"Security error.\n\nError message{ex.Message}\n\n" +
                $"Details:\n\n{ex.StackTrace}");
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我尝试使用我的课程时,都会出现错误 - Whenever I try to use my class, I get an error 当我尝试使用 ExcelDataReader 将文件上传到数据库时出现“不支持指定的方法”错误 - I get a "Specified method is not supported" error when i try to use ExcelDataReader to upload file to database 当我尝试保存没有图片或图像的数据时遇到问题。 我收到此消息:“空路径名不合法。” - I'm having a problem when I try to save the data without pictures or images. I get this message: "Empty path name is not legal." 我正在尝试为我的图标文件提供路径,但显示错误? - I am trying to give a path for my icon file but it shows an error? 我收到一条错误消息,显示“不支持给定路径的格式”。 尝试将音频文件上传到 sharepoint - I am getting an error that reads “The given path's format is not supported.” when trying to upload audio file to sharepoint 我正在使用HP fortify扫描代码。 尝试打开文件时出现路径处理错误 - I am scanning the code using HP fortify. I am getting Path Manipulation error when I try to open the File 为什么当我尝试使用 octokitnet 创建新文件时出现未找到错误? - Why i am getting a not found error when i try to create a new file with octokitnet? 为什么每次尝试发布项目时都会出错? - Why I get error whenever I try to publish the project? 每当我尝试连接数据库时都会收到错误消息 - I get an error whenever I try to connect database 我尝试读取.aspx文件时StreamReader文件路径错误 - StreamReader file path error when i try to read .aspx file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM