我使用以下代码来选择要在Windows窗体项目中导入的文件.
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtpath.Text = fdlg.FileName;
}
Run Code Online (Sandbox Code Playgroud)
问题是所选文件是在我不想要的后台打开的.如何在不打开文件的情况下获取所选文件的路径?
显示OpenFileDialog和用户选择文件不会打开文件.可以通过调用打开该文件OpenFile.在您发布的代码中,文件未打开.该代码似乎是从MSDN上的示例复制的.该示例中的其余代码如下:
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null) // File is opened here.
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " +
ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
如果在您不希望打开文件时打开文件,则问题必须在其他地方而不是您发布的代码中.例如,您可能在上次使用完文件后没有关闭文件(例如使用Dispose).
| 归档时间: |
|
| 查看次数: |
2107 次 |
| 最近记录: |