locked
save file and folder name directory to the xml file RRS feed

  • Question

  • Hello.

    i want save folder and file directory of database. then i want copy all the folder to another directory.

    here in browse button i save the database directory in a textbox:

            private void buttonBrowse_Click(object sender, EventArgs e)
            {
    
               DialogResult dr = openFileDialog1.ShowDialog();
    
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        textBoxFileName.Text = openFileDialog1.FileName;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }

    then i use a class(SettingsAccess) to put directory in to the xml file:

            private void buttonEnter_Click(object sender, EventArgs e)
            {
                if (textBoxFileName.Text != "")
                {
    
                        settings.FileName = textBoxFileName.Text;
                        //settings.FoldernameName = textBoxFileName.Text-mydatabase name
                        settingaccess.WriteSettings(settings);
    
                }
            }

    now i want save the folder of my database at first.

    for example:

    this is my database directory: c:\x\database.mdf

    i want save this directory too: c:\x

    then copy to another directory(something like Backup).

    if you need more than information about settings and SettingsAccess class let me know.




    Wednesday, March 28, 2012 7:15 AM

Answers

  • hi:)

    I didnt really got your problem.

    As I understand you want to cut from the path shown in the opendialog the c:\x isnt?

    mean you want to get the folder that is containing the database filename

    If im really understanding so this code below shows the directory name containing.

    string filepath = "C:\x\database.mdf";
    System.IO.FileInfo filename = new System.IO.FileInfo(filepath);
    MessageBox.Show(filename.Directory.Name);

    and to get the path from opendialog you use: openFileDialog.FileName

    Regards


    Best Regards...Please mark as answer if my post is helpful http://yosr-jemili.blogspot.com


    • Edited by YosrJ Thursday, March 29, 2012 5:37 AM
    • Marked as answer by Bouki Thursday, March 29, 2012 7:16 AM
    Thursday, March 29, 2012 5:36 AM

All replies

  • Hi

    In order to copy a file from a path to another path you should use file.copy

    File.Copy("c:\x\database.mdf", "c:\x\", true);

    Regards


    Best Regards...Please mark as answer if my post is helpful http://yosr-jemili.blogspot.com


    • Edited by YosrJ Wednesday, March 28, 2012 8:55 AM
    Wednesday, March 28, 2012 8:55 AM
  • yes i know.but at first i need to save the directory in a xml file.

    as i say in my second code.

    please read my first post again.

    Wednesday, March 28, 2012 9:06 AM
  • i want create a xml file such as:

      <FileName>C:\Users\Desktop\DataBase.mdf</FileName>
      <FolderName>C:\Users\Desktop</FolderName>


    just it.

    i should write something else here in the second line:

                       settings.FileName = textBoxFileName.Text;
                        //settings.FoldernameName = textBoxFileName.Text- (mydatabase name)(here i should use something like this) 
                        settingaccess.WriteSettings(settings);
    

     

    • Edited by Bouki Wednesday, March 28, 2012 2:10 PM
    Wednesday, March 28, 2012 2:05 PM
  • Hi

    Try this way

                XmlTextWriter writer = new XmlTextWriter("xmlnamefile.xml", System.Text.Encoding.UTF8);
                writer.WriteStartDocument(true);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 2;
                 writer.WriteStartElement("FiLeName");
                writer.WriteString("C:\users\desktop\database.mdf");
                writer.WriteEndElement();
                writer.WriteStartElement("FolderName");
                writer.WriteString("C:\users\desktop\");
                writer.WriteEndElement();
    Regards


    Best Regards...Please mark as answer if my post is helpful http://yosr-jemili.blogspot.com


    • Edited by YosrJ Wednesday, March 28, 2012 2:29 PM
    Wednesday, March 28, 2012 2:29 PM
  • oooops. no. :D

    i think i cant explain what i want to do.

    i have a form. in this form user select the database file(with openfiledialog) then this directory(database location) save in the xml file.

    its work perfect. but now i want save the folder directory which database file is in there.

    for example:

    this is my database directory: c:\x\database.mdf

    user select this directory with openfiledialog now how can Separate to two part:

    1- c:\x\database.mdf

    2- c:\x

    my database name is variable. it can be anything.

    i can create xml file easily. its not a problem.

    i wish i could explain.

    Wednesday, March 28, 2012 8:25 PM
  • hi:)

    I didnt really got your problem.

    As I understand you want to cut from the path shown in the opendialog the c:\x isnt?

    mean you want to get the folder that is containing the database filename

    If im really understanding so this code below shows the directory name containing.

    string filepath = "C:\x\database.mdf";
    System.IO.FileInfo filename = new System.IO.FileInfo(filepath);
    MessageBox.Show(filename.Directory.Name);

    and to get the path from opendialog you use: openFileDialog.FileName

    Regards


    Best Regards...Please mark as answer if my post is helpful http://yosr-jemili.blogspot.com


    • Edited by YosrJ Thursday, March 29, 2012 5:37 AM
    • Marked as answer by Bouki Thursday, March 29, 2012 7:16 AM
    Thursday, March 29, 2012 5:36 AM
  • yes.

    you understand well. and thank you so much.


    • Edited by Bouki Thursday, March 29, 2012 7:16 AM
    Thursday, March 29, 2012 6:59 AM