Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i add two paths in app.config file

XML
<appSettings>
   <add key="abc" value="C:\"/>
   <add key="def" value="D:\"/>
 </appSettings>


What I have tried:

<appsettings>
<add key="teltonikaPath" value="W:\">
<add key="lmuPath" value="X:\">

then when i call abc in code
try
C#
{
    string path = "";
    path = ConfigurationManager.AppSettings.Get("abc");
    DateTime dt = File.GetLastAccessTime(path);
    Console.WriteLine("the last time {0}", dt);

}
catch (Exception e)
{
    Console.WriteLine("The process failed: {0}", e.ToString());
}
Thread.Sleep(5000);

then how i call second path def

any solution
Posted
Updated 10-May-16 21:09pm
v3
Comments
Karthik_Mahalingam 11-May-16 2:13am    
path = ConfigurationManager.AppSettings.Get("def");
super_user 11-May-16 2:17am    
if i add this then path same name exist in this line
path = ConfigurationManager.AppSettings.Get("abc");
Suvendu Shekhar Giri 11-May-16 2:18am    
Can you please share the specific section of the app.config?
super_user 11-May-16 2:22am    
i already share
super_user 11-May-16 2:23am    
and when i use this path = ConfigurationManager.AppSettings.Get("def"); then how i i identity data when i run this application

the gumby way to do what you want would be like

C#
{
    string path_abc = "";
    path_abc = ConfigurationManager.AppSettings.Get("abc");
    DateTime dt_abc = File.GetLastAccessTime(path_abc);
    Console.WriteLine("the last access time for  {0} is {1}", path_abc, dt_abc);
 
    string path_def = "";
    path_def = ConfigurationManager.AppSettings.Get("def");
    DateTime dt_def = File.GetLastAccessTime(path_def);
    Console.WriteLine("the last access time for {0} is {1}", path_def, dt_def);
}
catch (Exception e)
{
    Console.WriteLine("The process failed: {0}", e.ToString());
}
Thread.Sleep(5000);


but thats a repeat .. boring ... all this

Quote:
{
string path = "";
path = ConfigurationManager.AppSettings.Get("abc");
DateTime dt = File.GetLastAccessTime(path);
Console.WriteLine("the last time {0}", dt);

}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}


should be ripped out and put in a public static function that returns a DateTime and takes the name of the 'key' in the config file as a string to work with, with appropriate error handling etc - you'd have to decide what to return if the path didnt exist for example
 
Share this answer
 
As others pointed out in comments, you have just to duplicate the code that already handles the "abc" case, or , better, write a method having a string as parameter, e.g.
C#
void ShowLastAccessTime(String key)
{
  //...
}
 
Share this answer
 
v2
Comments
Garth J Lancaster 11-May-16 2:59am    
beat me to it :-)
super_user 11-May-16 3:17am    
please explain more little bit
try below code
C#
{
    string pathAbc, pathDef  = "";
    pathAbc = ConfigurationManager.AppSettings.Get("abc");
    pathDef  = ConfigurationManager.AppSettings.Get("def");

    DateTime dtAbc = File.GetLastAccessTime(pathAbc);
    DateTime dtDef = File.GetLastAccessTime(pathDef);


    Console.WriteLine("the last time ", dtAbc.ToString("MM/dd/yyyy hh:MM:ss"));
    Console.WriteLine("the last time ", dtDef.ToString("MM/dd/yyyy hh:MM:ss"));
 
}
catch (Exception e)
{
    Console.WriteLine("The process failed: {0}", e.ToString());
}
Thread.Sleep(5000);


Basically GetLastAccessTime Returns the date and time the specified file or directory was last accessed, as system (C:\\) frequently access C:\Temp folder and if you are running code on D:\ then it also access D:\ at same time so I think there should be more time difference in both the cases
 
Share this answer
 
v2
Comments
super_user 11-May-16 3:22am    
it shows error Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
koolprasad2003 11-May-16 3:30am    
I have update solution, now you can try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900