绝对路径和相对路径之间有什么区别? [英] What is the difference between an absolute and a relative path?

查看:105
本文介绍了绝对路径和相对路径之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之所以问是因为我正在为一个学校的项目工作.是的,这是家庭作业.但是,我想进一步了解一点.

I am asking because I am working on a project for school. Yes this is homework. But, I'm trying to understand a little bit more, though.

这是被问到的一个例子.

This is one example of what is being asked.

•当用户单击保存"按钮时,将所选记录写入txtFilePath中指定的文件(绝对路径不是相对路径)中,而不会截断当前其中的值.

• When the user clicks the "Save" button, write the selected record to the file specified in txtFilePath (absolute path not relative) without truncating the values currently inside.

这就是我所拥有的

private void button2_Click(object sender, EventArgs e)
{
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        StreamWriter myWriter = new StreamWriter(saveFileDialog1.FileName);
        myWriter.Write(txtFilePath.Text);
        myWriter.Close();
    }
}

现在,我不知道我是否做对了.我知道当我将其保存到桌面并从列表框中删除它时,当我尝试重新加载它时,什么也没有出现.这就是表格上的内容,

Now, I don't understand if I am doing this right. I know when I save it to my desktop and I delete it from my listbox and when I try to reload it again nothing shows up. This is what I have on my form,

private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        StreamReader myReader = new StreamReader(openFileDialog1.FileName);
        txtFilePath.Text = openFileDialog1.FileName;
        txtFilePath.Text = myReader.ReadToEnd();
        myReader.Close();
    }
}    

这是负担,

private void Form1_Load(object sender, EventArgs e)
{
    string[] myFiles = Directory.GetFiles("C:\\");
    foreach (string filename in myFiles)
    {
        FileInfo file = new FileInfo(filename);
        employeeList.Items.Add(file.Name);
    }

    //...

有人可以帮我弄清楚这一点吗?

Can someone please help me make sense of this?

推荐答案

假设您正在指示景点.您可以通过两种方法描述到达该位置的方法:

Say you were giving directions to a spot. You have two methods you can describe getting to the location:

  • 相对于您站立的位置
  • 相对于地标.

两者都将您带到同一个位置,但是前者总是不起作用(先左走,然后走右,穿过两盏灯,然后再走右行",不一定能用)从下一个城镇开始,但从您所在的位置开始工作).本质上就是区别.

Both get you to the same location, but the former doesn't always work ("take a left, then a right, go through two lights then take another right" wouldn't necessarily work from the next town over, but works from where you stand). That's essentially the difference.

如果有C:\Windows\System32,则这是绝对路径.如果您有Windows\System32,则只有从C:\开始启动时,它才有效.如果从C:\Program Files开始,则需要..\才能正确到达那里.

If you have C:\Windows\System32, that's an absolute path. If you have Windows\System32, it will only work so long as you're starting from C:\. If you start in C:\Program Files you would need a ..\ to get there correctly.

但是,无论您位于硬盘驱动器上的什么位置,C:\Windows\System32\都是访问该文件夹的明确方法.

However, no matter where you are on the hard drive, C:\Windows\System32\ is a definitive way to get to that folder.

这篇关于绝对路径和相对路径之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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