【问题标题】:Simplified path during coding编码过程中的简化路径
【发布时间】:2014-07-01 07:00:44
【问题描述】:

如何简化C:\Users\Administrator\\Desktop\\asd.txt 的路径,因为我想让位置字符串通过文本框输入。这是我制作的代码。我想让其他人更容易搜索路径。

这是错误。

在 mscorlib.dll 中发生“System.IO.DirectoryNotFoundException”类型的第一次机会异常

附加信息:找不到路径“C:\Users\khairishafiq\Desktop\hash application v1.5\hashapplication\bin\Debug\using System.Collections.Generic;”的一部分。

如果有这个异常的处理程序,程序可以安全地继续。

代码:

    string location = "C:\\Users\\Administrator\\Desktop\\asd.txt";
    int i = 0;
    public Form1()
    {
        InitializeComponent();
    }

    public void cuba()
    {
        var desiredText = File.ReadLines(location).ElementAt(i);
        string s = desiredText.ToString();
        textBox3.Text = s;
        Regex r = new Regex(@"\[(.+?)\]");
        MatchCollection mc = r.Matches(s);
        textBox1.Text= mc[0].Groups[1].Value;
        textBox2.Text= mc[1].Groups[1].Value;
        textBox3.Text = mc[2].Groups[1].Value;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        cuba();
    }

【问题讨论】:

  • 为什么没有一个可以打开FileOpenDialog的按钮/菜单项?您可以随时提供一个默认目录来打开它
  • 您不是管理员帐户。你有访问权限吗?

标签: c# path


【解决方案1】:

如果你的意思是:获取桌面的位置,你可以使用这段代码:

string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

那么location就是,当文件名asd.txt固定时:

string location = Path.Combine(desktopFolder, @"asd.txt");

如果没有,你应该使用一些东西让用户自己选择文件,例如OpenFileDialog:

OpenFileDialog openFileDialog1 = new OpenFileDialog();

if (openFileDialog1.ShowDialog == DialogResult.OK)
{
    string location = openFileDialog1.FileName;
}

【讨论】:

  • 其实就是一个例子。我正在制作的应用程序允许用户手动插入路径(从电脑中的任何地方),但是当插入正常路径时,会发生错误。它需要双反斜杠来识别路径。
  • 那么问题出在哪里?你的问题不清楚。你的问题是正则表达式吗?
猜你喜欢
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2012-11-04
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多