|
-
November 25th, 2008, 02:53 AM
#1
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
-
November 25th, 2008, 05:35 AM
#2
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]
-
November 25th, 2008, 06:28 AM
#3
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?
-
November 25th, 2008, 06:55 AM
#4
Re: File Path in XML
 Originally Posted by shers
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));
}
-
November 25th, 2008, 06:57 AM
#5
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]
-
November 25th, 2008, 07:01 AM
#6
Re: File Path in XML
 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]
-
November 25th, 2008, 07:05 AM
#7
Re: File Path in XML
 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]
-
January 5th, 2009, 06:04 AM
#8
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.
-
January 5th, 2009, 06:12 AM
#9
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
-
January 5th, 2009, 06:17 AM
#10
Re: File Path in XML
 Originally Posted by toraj58
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)
-
January 5th, 2009, 07:36 AM
#11
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]
-
January 7th, 2009, 02:42 AM
#12
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
-
January 7th, 2009, 02:45 AM
#13
Re: File Path in XML
 Originally Posted by shers
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.
-
January 7th, 2009, 07:47 AM
#14
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]
-
January 7th, 2009, 06:02 PM
#15
Re: File Path in XML
 Originally Posted by toraj58
@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#.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|