none
absolute and relative paths RRS feed

  • Question

  • I am having trouble trying to understand and code this

    
    
    • When the button “Load” is clicked, read the file specified in the textbox (txtFilePath: Absolute path not relative) and add the objects found within to the listbox
    • 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

    Now I am adding to a project I already did this is what I have.
     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void addButton_Click(object sender, EventArgs e)
            {
                EditDialog newEmployeeDialog = new EditDialog();
                if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
                {
                    employeeList.Items.Add(newEmployeeDialog.StaffMember);
                }
            }
    
            private void deleteButton_Click(object sender, EventArgs e)
            {
                if (employeeList.SelectedIndex == -1)
                    return;
    
                if (MessageBox.Show("Really delete this employee",
                    "Delete", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question)
                == DialogResult.Yes)
                {
                    employeeList.Items.Remove(
                        employeeList.SelectedItem);
                }
            }
    
            private void editButton_Click(object sender, EventArgs e)
            {
                if (employeeList.SelectedIndex == -1)
                    return;
    
                int employeeNum = employeeList.SelectedIndex;
                EditDialog newEmployeeDialog = new EditDialog();
                newEmployeeDialog.StaffMember =
                    (Employee)employeeList.SelectedItem;
    
                if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
                {
                    employeeList.Items.RemoveAt(employeeNum);
                    employeeList.Items.Insert(employeeNum, newEmployeeDialog.StaffMember);
                    employeeList.SelectedIndex = employeeNum;
                }
            }
    
            private void employeeList_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (employeeList.SelectedIndex != -1)
                {
                    Employee currentEmployee = (Employee)employeeList.SelectedItem;
                    firstName.Text = currentEmployee.FirstName;
                    lastName.Text = currentEmployee.LastName;
                    jobTitle.Text = currentEmployee.JobTitle;
                }
                else
                {
                    firstName.Text = lastName.Text = jobTitle.Text = "";
                }
            }
    
    
            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();
                }
                saveFileDialog1.FileName = openFileDialog1.FileName.ToString();
            }
                
    
            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);
                }
            }
    
            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();
                }
    
            }
            }
    
        }
          

    Now I am not understanding what I am doing wrong or I am reading to much and over thinking.

    Tuesday, April 24, 2012 12:54 AM

Answers

  • Hi sha1023012,
    FileDialog.FileName Property will return the file name selected in the file dialog box. The file name includes both the file path and the extension, so it is an absolute path.
    I’m not sure what do you mean by “I am suppose to use absolute path for the load and save buttons. But not suppose to use save dialog or open dialog “  here.  Do you mean that StreamReader will accept a relative path and you want it accept absolute path only?
    If so, you can either check if the string contains a root by using Path.IsPathRooted Method or get the absolute path for the string by using Path.GetFullPath Method and compare the full path with the original string.

    private void button1_Click(object sender, EventArgs e) {

    //it's better to add a try catch here

    // workaround1 if (txtFilePath.Text == null) { MessageBox.Show("path can't be null"); return; } //The IsPathRooted method returns true if the first character is a directory separator character such as "\", or if the path starts with a drive letter and colon (:). if ((!Path.IsPathRooted(txtFilePath.Text)) || (txtFilePath.Text.StartsWith("\\") && !txtFilePath.Text.StartsWith("\\\\"))) { MessageBox.Show("please enter a absolute path"); return; } //do you staff //workaround 2 if(txtFilePath.Text == null) { MessageBox.Show("path can't be null"); return; } if (!Path.GetFullPath(txtFilePath.Text).Equals(txtFilePath.Text)) { MessageBox.Show("please enter a absolute path"); return; } //do you staff }


    If you still have any doubt and concern about this issue, please let us know. If I misunderstood you, please kindly elaborate your question.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by sha1023012 Wednesday, April 25, 2012 10:55 PM
    Wednesday, April 25, 2012 2:39 AM
    Moderator

All replies

  • Probably the details led to more confusions.

    My queries are following:

    1. Where is the load button (do you mean button1_click)

    2. Why the following lines are assigning to the same text box?

                    txtFilePath.Text = openFileDialog1.FileName;
                    txtFilePath.Text = myReader.ReadToEnd();

    3. What is the exact problem you are facing?


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Tuesday, April 24, 2012 6:44 AM
  • to answer your questions.

    1. Is yes.

    2. I dont know I am kinda confused about this.

    3. What I am not understanding is I am suppose to use absolute path for the load and save buttons. But not suppose to use save dialog or open dialog . So I am not really understanding how else is there to do what is being asked.

    Tuesday, April 24, 2012 10:06 AM
  • Hi sha1023012,
    FileDialog.FileName Property will return the file name selected in the file dialog box. The file name includes both the file path and the extension, so it is an absolute path.
    I’m not sure what do you mean by “I am suppose to use absolute path for the load and save buttons. But not suppose to use save dialog or open dialog “  here.  Do you mean that StreamReader will accept a relative path and you want it accept absolute path only?
    If so, you can either check if the string contains a root by using Path.IsPathRooted Method or get the absolute path for the string by using Path.GetFullPath Method and compare the full path with the original string.

    private void button1_Click(object sender, EventArgs e) {

    //it's better to add a try catch here

    // workaround1 if (txtFilePath.Text == null) { MessageBox.Show("path can't be null"); return; } //The IsPathRooted method returns true if the first character is a directory separator character such as "\", or if the path starts with a drive letter and colon (:). if ((!Path.IsPathRooted(txtFilePath.Text)) || (txtFilePath.Text.StartsWith("\\") && !txtFilePath.Text.StartsWith("\\\\"))) { MessageBox.Show("please enter a absolute path"); return; } //do you staff //workaround 2 if(txtFilePath.Text == null) { MessageBox.Show("path can't be null"); return; } if (!Path.GetFullPath(txtFilePath.Text).Equals(txtFilePath.Text)) { MessageBox.Show("please enter a absolute path"); return; } //do you staff }


    If you still have any doubt and concern about this issue, please let us know. If I misunderstood you, please kindly elaborate your question.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by sha1023012 Wednesday, April 25, 2012 10:55 PM
    Wednesday, April 25, 2012 2:39 AM
    Moderator