如何从打开文件对话框中获取文件路径 [英] How to get a file path from the open file dialog box

查看:74
本文介绍了如何从打开文件对话框中获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个涉及首次使用打开文件对话框的程序。我目前将streamreader设置为打开特定文件并将该文件输出到列表。但我想添加更改该文件的功能。我已将其设置为打开第一个文件所在的特定位置的对话框。然后,当他们选择新文件时,我想用新目录替换streamreader的目录,用一个单独的按钮打开它。我认为最简单的方法是设置一个字符串来保存所选路径,然后将streamreader中的路径分配给字符串。如何将他们选择的路径分配给字符串?或者有更简单的方法吗?

 命名空间 DNA 
{
public partial class frmDNA:Form
{
StreamReader DNAFile = new StreamReader( / documents / visual studio 2013 / Projects / DNA / DNA.txt);
int 索引;
int Lines = File.ReadLines( / documents / visual studio 2013 / Projects / DNA / DNA.txt)。Count();
String DNA,Path;
public frmDNA()
{
InitializeComponent();
}

private void btnOpen_Click( object sender,EventArgs e)
{
OpenFileDialog openDNAfile = new OpenFileDialog();
openDNAfile.InitialDirectory = / documents / visual studio 2013 / Projects / DNA / DNA.txt;
openDNAfile.Filter = txt文件(* .txt)| * .txt |所有文件(*。 。*)| *。*;
openDNAfile.FilterIndex = 2 ;
openDNAfile.RestoreDirectory = true ;
if (openDNAfile.ShowDialog()== DialogResult.OK)
{
尝试
{
Path = // 我不知道该放什么这里
}
}
}





请注意还有一些额外的东西在这里代码的其他部分不影响此方法。快速回复将不胜感激。在此先感谢!

解决方案

您无需担心: openDNAfile.FileName ,一旦选择,对话框关闭后(表示未取消),始终为完整路径



-SA


你好,



尝试使用此代码:



 OpenFileDialog openFileDialog =  new  OpenFileDialog(); 
DialogResult dialogResult = openFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string fileName = openFileDialog.FileName;
尝试
{
.txtLocation.Text =系统。 IO.Path.GetFullPath(文件名);
}
catch (System.Exception)
{
}
}





尝试也尝试一下。欢呼声。


I'm creating a program that involves using the open file dialog box for the first time. I currently have the streamreader set to open a specific file and output that file to a list. But i want to add the ability to change that file. I have it set to open the dialog to the specific location the first file is at. Then when they choose their new file i want to replace the directory for the streamreader with the new directory to open it with a separate button. I think the easiest way to do this is to set a string to hold the chosen path then assign the path in the streamreader to the string. How can i assign the path they choose to a string? or is there an easier way to do this?

namespace DNA
{
    public partial class frmDNA : Form
    {
        StreamReader DNAFile = new StreamReader("/documents/visual studio 2013/Projects/DNA/DNA.txt");
        int Index;
        int Lines = File.ReadLines("/documents/visual studio 2013/Projects/DNA/DNA.txt").Count();
        String DNA, Path;
        public frmDNA()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDNAfile = new OpenFileDialog();
            openDNAfile.InitialDirectory = "/documents/visual studio 2013/Projects/DNA/DNA.txt";
            openDNAfile.Filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
            openDNAfile.FilterIndex = 2;
            openDNAfile.RestoreDirectory = true;
            if (openDNAfile.ShowDialog() == DialogResult.OK) 
            {
                try
                {
                  Path = // I don't know what to put here
                }
            }
        }



Please note there is some extra stuff in here for other parts of the code that don't affect this method. Quick replies would be appreciated. Thanks in advance!

解决方案

There is nothing you have to worry about: openDNAfile.FileName, once chosen, after the dialog is closed (it means, not cancelled), is always full path.

—SA


Hi there,

try using this code right here:

OpenFileDialog openFileDialog = new OpenFileDialog();
			DialogResult dialogResult = openFileDialog.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				string fileName = openFileDialog.FileName;
				try
				{
					this.txtLocation.Text = System.IO.Path.GetFullPath(fileName);
				}
				catch (System.Exception)
				{
				}
			}



Try to experiment with that as well. Cheers.


这篇关于如何从打开文件对话框中获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆