CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Join Date
    Apr 2004
    Posts
    265

    File Path in XML

    Hi all,

    I'm creating a progam in C# that reads a file location at start up and displays the parent folders in a combo box. This file location may change in future. So I intend to keep this in an xml file and allow the user to change it when needed. But how do I go about it? I have not done any coding with xml yet. So, please help.

    Thanks

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: File Path in XML

    there is no need to use XML for this issue;
    XML is mostly used for Tree-Like data;
    instead i suggest you use such code:

    i used simple logic in my code that you can use it and apply it to any Console or Windows app.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
    namespace FileReadWrite
    {
        class Program
        {
            static void Main(string[] args)
            {
                SavePath(@"c:\myfolder2");
    
                string appPath = GetPath();
    
                if (appPath != String.Empty)
                {
                    Console.WriteLine(appPath);
                }
                else
                {
                    Console.WriteLine("app.log file does not exists!");
                }
    
    
                Console.ReadLine();
    
            }
    
            public static void SavePath(string pathName)
            {
                File.WriteAllText("app.log", pathName);
            }
    
            public static string GetPath()
            {
                if (File.Exists("app.log"))
                {
                    string strPath = File.ReadAllText("app.log");
                    return strPath;
                }
                else
                return String.Empty;
            }
        }
    }
    Last edited by toraj58; November 25th, 2008 at 05:40 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Apr 2004
    Posts
    265

    Re: File Path in XML

    Yes, this is the issue. If, in future, I need to change the path from "c:\myfolder2" to "c:\myfolder1", how will the user do it without opening the source code?

  4. #4
    Join Date
    Nov 2008
    Posts
    15

    Re: File Path in XML

    Quote Originally Posted by shers View Post
    Yes, this is the issue. If, in future, I need to change the path from "c:\myfolder2" to "c:\myfolder1", how will the user do it without opening the source code?
    well, just dont hardcode it, and make a FolderBrowserDialog to let the user choose a path;

    Code:
    //need this for the dialog  
    using System.Windows.Forms;  
      
    FolderBrowserDialog Finder = new FolderBrowserDialog();  
      
    //dialog title  
    Finder.Description = "find a folder";  
      
    //you can hide the abillity to create new folders  
    Finder.ShowNewFolderButton = false;  
      
    //this is where the dialog starts  
    Finder.RootFolder = System.Environment.SpecialFolder.Desktop;  
      
    //here we could call the dialog by Finder.ShowDialog();  
    //or make an if-statement to check if a folder was choosen  
    if ((Finder.ShowDialog() == DialogResult.OK) && Finder.SelectedPath != null)  
    {  
        string path = Finder.SelectedPath;  
    }
    and if you want to move all the content just copy everything from the first folder recursively to the next.

    Code:
    public static void RecCopy(string source, string destination)
    {
        // Create the destination folder if it's not there.
        if (!Directory.Exists(destination))
        {
            Directory.CreateDirectory(destination);
        }
        DirectoryInfo dirInfo = new DirectoryInfo(source);
    
        // Copy all files.
       foreach (FileInfo fileInfo in dirInfo.GetFiles())
            fileInfo.CopyTo(Path.Combine(destination, fileInfo.Name), true);
    
        // Recursively copy all sub-directories.
        if (dirInfo.GetDirectories().GetLength(0) > 1)
        {
            foreach (DirectoryInfo subDirectoryInfo in dirInfo.GetDirectories())
                RecCopy(subDirectoryInfo.FullName, Path.Combine(destination, subDirectoryInfo.Name));
    }

  5. #5
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: File Path in XML

    well;

    change this line

    SavePath(@"c:\myfolder2");

    to

    Code:
    string pathStr;
    Console.Write("Please enter new path:");
    pathStr = Console.ReadLine();
    SavePath(pathStr);
    that is when you use ConsoleApp but if your application is windows app with GUI then you can use some control like TextBox to get the path from the user then save it in the file.

    Code:
    string pathStr = textBox1.Text;
    SavePath(pathStr);
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  6. #6
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: File Path in XML

    Quote Originally Posted by Jensecj
    well, just dont hardcode it, and make a FolderBrowserDialog to let the user choose a path;
    yes, FolderBrowserDialog is another good approach.
    Last edited by toraj58; November 25th, 2008 at 07:07 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  7. #7
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: File Path in XML

    Quote Originally Posted by Jensecj
    FolderBrowserDialog Finder = new FolderBrowserDialog();
    Hint: always let your objects begin with lower case; i think it is called Hungerian Naming Standard.

    Code:
    FolderBrowserDialog finder = new FolderBrowserDialog();
    Last edited by toraj58; November 25th, 2008 at 07:08 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  8. #8
    Join Date
    Apr 2004
    Posts
    265

    Re: File Path in XML

    I'm sorry, I don't agree to this method of FolderBrowser, coz everytime the user opens the app, he has to browse to the location to run the app. So, is there a way to allow the user to change the location when required, as it happens once in a blue moon.

  9. #9
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: File Path in XML

    Use the Settings file! It's what it was invented for..

    http://msdn.microsoft.com/en-us/libr...69(VS.80).aspx
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  10. #10
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: File Path in XML

    Quote Originally Posted by toraj58 View Post
    Hint: always let your objects begin with lower case; i think it is called Hungerian Naming Standard.
    http://en.wikipedia.org/wiki/Hungarian_notation
    Not a standard, and not really necessary in today's apps. It is also unrelated to the convention of putting the first letter of an instance variable in lowercase. That is called camelCase (vs PascalCase, UPPERCASE, Titlecase and lowercase)


    See
    http://msdn.microsoft.com/en-us/library/ms229045.aspx
    and
    http://poke53280.net/Programming/Standards.aspx (http://www.irritatedvowel.com/Progra...Standards.aspx)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  11. #11
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: File Path in XML

    i don't think that for this case we should refer to wikipedia.

    this is a global convention between programmers that they do like this:

    1- Begin the first letter of a class with Capital Letter; e.g.: Class Foo
    2- Begin the first letter of the object with lowercase; e.g.: Foo foo1 = new Foo();
    3- Begin a constant with all Upper Case Letters; e.g.: const MAXARRAYNUM = 100;
    4- Naming Methods like CamelCase; e.g.: WriteLine()
    5- Begin Interface name with capital I like IDisposable
    6- beging bool with Is like IsNumeric

    and bela bela.

    some companies applay their own naming convenntion to code and make all coders adhere to it.

    but when you code for yourself or others to help them it would be better that you notice to these naming conventions.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: File Path in XML

    In C# the general convention is as follows:

    local variables - camelCase
    method parameters - camelCase
    private fields - camelCase (usually prefixed with '_', e.g. _myField)
    Properties - PascalCase
    Methods - PascalCase
    Class names - PascalCase
    Constants - PascalCase

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: File Path in XML

    Quote Originally Posted by shers View Post
    I'm sorry, I don't agree to this method of FolderBrowser, coz everytime the user opens the app, he has to browse to the location to run the app. So, is there a way to allow the user to change the location when required, as it happens once in a blue moon.
    The first time the user uses the functionality, prompt him/her with the browse dialog, then save the path into the settings file. In addition, give the user the option to change the path in the future and display the browse dialog.

  14. #14
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: File Path in XML

    @Arjay: all you have mentioned is correct but i think constants should be all upper case in c++ and c# languages (i always adhere to this convention). isn't it?
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: File Path in XML

    Quote Originally Posted by toraj58 View Post
    @Arjay: all you have mentioned is correct but i think constants should be all upper case in c++ and c# languages (i always adhere to this convention). isn't it?
    In C++ yes, but not in C#.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured