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?
An absolute path includes the local hard drive letter or the network server drive letter. This is the most specific but least flexible option. Relative paths are partially specified folder paths that assume the current drive letter or the folder of the host drawing.
For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string. A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path.
Summary: 1. Relative is always in proportion to a whole. Absolute is the total of all existence.
An absolute file path describes how to access a given file or directory, starting from the root of the file system. A file path is also called a pathname. Relative file paths are notated by a lack of a leading forward slash. For example, example_directory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With