What is the difference between an absolute and a relative path?

十年热恋 提交于 2019-12-09 02:34:53

问题


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.

• 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.

This is what I have,

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();
    }
}    

And this is the load,

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?


回答1:


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

  • Relative to where you stand,
  • Relative to a landmark.

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.

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.

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




回答2:


It's actually a simple distinction. A relative file path is going to be a structure based around a root node; and an absolute path is going to be a structure based on a non ambiguous location. That sounds kind of wonky, but it's actually pretty simple.

Here are some examples:

Absolute Paths

 C:\inetpub\yourapplication\default.aspx
 http://www.yourapplication.com/default.aspx

These paths are absolute because they are non ambiguous. Example 1 shows an absolute file path, and example 2 shows an absolute URL.

Relative Paths

./../script/something.js
~/default.aspx

A relative path specifies a location based on some known ahead point of reference. So in example 1, you know to go up one directory, then down into a directory called script, then to a javascript file. In example two, you are specifing the aspx page contained within the root of your application.

So, germane to your specific problem, you want to write a file to a specific absolute path, which means it needs to be a non ambiguous location.




回答3:


An absolute path is the whole path name required to access the location in the file system.
For example: C:\Program Files\Internet Explorer\iexplorer.exe

Where as a relative path is in relation to some landmark, usually your main executable files location or the 'start in' location set when you open the program.

For example if your main executable is in C:\Program Files\ the relative path to iexplorer.exe is Internet Explorer\iexplorer.exe.

This is done usually when you don't always know where the file will absolutely be, like which drive letter it will be installed in or which folder it will be under.

However for a good example, if your file came with your program and you know your programs installation structure, you can use relative pathing to find all your files no matter where your program is installed as opposed to abolute pathing where your program would need to be installed in the exact same location each time.



来源:https://stackoverflow.com/questions/10288223/what-is-the-difference-between-an-absolute-and-a-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!