app.config文件中的路径 [英] Path in app.config file

查看:431
本文介绍了app.config文件中的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在app.config文件中添加两个路径



 <   appSettings  >  
< add key = abc = C:\ / >
< add key = def value <跨度cl ass =code-keyword> = D:\ / >
< / appSettings >





我的尝试:



< appsettings>

< add key =teltonikaPathvalue =W:\>

< add key =lmuPathvalue =X:\>



然后当我在代码中调用abc时

尝试

 {
string path = ;
path = ConfigurationManager.AppSettings.Get( abc);
DateTime dt = File.GetLastAccessTime(path);
Console.WriteLine( 最后一次{0},dt);

}
catch (例外e)
{
Console.WriteLine( 进程失败:{0},e.ToString());
}
Thread.Sleep( 5000 );



然后我如何调用第二条路径def



任何解决方案

解决方案

以你想要的方式做傻瓜的方式就好了



 {
string path_abc = ;
path_abc = ConfigurationManager.AppSettings.Get( abc);
DateTime dt_abc = File.GetLastAccessTime(path_abc);
Console.WriteLine( {0}的最后访问时间为{1} ,path_abc,dt_abc);

string path_def = ;
path_def = ConfigurationManager.AppSettings.Get( def);
DateTime dt_def = File.GetLastAccessTime(path_def);
Console.WriteLine( {0}的最后访问时间为{1} ,path_def,dt_def);
}
catch (例外e)
{
Console.WriteLine( 进程失败:{0},e.ToString());
}
Thread.Sleep( 5000 );





但那是重复......无聊......所有这一切



引用:

{

string path =;

path = ConfigurationManager.AppSettings.Get(abc);

DateTime dt = File.GetLastAccessTime(路径);

Console.WriteLine(最后一次{0},dt);



}

catch(例外e)

{

Console.WriteLine(进程失败:{0},e.ToString());

}





应该被删除并放入一个公共静态函数,该函数返回一个DateTime并取名为'在配置文件中键入'作为要使用的字符串,具有适当的错误处理等 - 如果路径不存在,则必须决定返回什么例如


正如其他人指出的那样在评论中,你只需复制鳕鱼已经处理abc的情况,或者更好的是,写一个字符串作为参数的方法,例如

  void  ShowLastAccessTime( String 键)
{
// ...
}


< blockquote>尝试下面的代码

 {
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( 最后一次, dtAbc.ToString( MM / dd / yyyy hh:MM:ss));
Console.WriteLine( 最后一次,dtDef.ToString( MM / dd / yyyy hh:MM:ss));

}
catch (例外e)
{
Console.WriteLine( 进程失败:{0},e.ToString());
}
Thread.Sleep( 5000 );





基本上GetLastAccessTime返回上次访问指定文件或目录的日期和时间,因为系统(C:\\)经常访问C:\ Temp文件夹,如果您在D:\上运行代码,那么它也同时访问D:\所以我认为两种情况都应该有更多的时差


i add two paths in app.config file

<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

{
    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

解决方案

the gumby way to do what you want would be like

{
    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


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.

void ShowLastAccessTime(String key)
{
  //...
}


try below code

{
    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


这篇关于app.config文件中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆