c# - 打开文件对话框中的strFileName

标签 c# openfiledialog

我是C#的新手。

我的问题是打开文件对话框中的strFileName是什么?

我目前有此代码:

 string input = string.Empty;

        OpenFileDialog open = new OpenFileDialog();

        open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

        open.InitialDirectory = "C:";

        if (open.ShowDialog() == DialogResult.OK)

            strFileName = open.FileName;

        if (strFileName == String.Empty)

            return; 


它在strFileName上显示错误。我找不到有关此代码中功能的解释。

如有任何帮助,我们将不胜感激,如果您之前曾提出此问题,我深表歉意。

最佳答案

不知道错误是什么,仅通过查看代码,您可能会在strFileName上收到编译错误,因为未声明该错误:

您可以将代码更改为此:

string input = string.Empty;

OpenFileDialog open = new OpenFileDialog();

open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

open.InitialDirectory = "C:";

if (open.ShowDialog() == DialogResult.OK)

   input = open.FileName;

if (input == String.Empty)

   return; 


或这个:

string strFileName = string.Empty;

OpenFileDialog open = new OpenFileDialog();

open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";

open.InitialDirectory = "C:";

if (open.ShowDialog() == DialogResult.OK)

   strFileName = open.FileName;

if (strFileName == String.Empty)

   return; 

关于c# - 打开文件对话框中的strFileName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203815/

相关文章:

c# - asp.net数据错误

ruby-on-rails - 从Ruby on Rails中的文件选择器对话框中获取文件的路径

c# - 继续打开 OpenFileDialog 直到选择有效文件

C# Enum 需要三元转换吗?

c# - C#自动添加作者姓名

c# - 为什么 TargetInvocationException 被视为未被 IDE 捕获?

winforms - C# OpenFileDialog 在 Win7 中只显示 XP-Style

Excel 文件在使用后保持锁定状态以供编辑

c# - openfiledialog 的过滤属性不起作用

c# - 将 Html 转换为 Pdf 的最快方法是什么?