writeline所有特殊文件夹 [英] writeline all specialfolders

查看:87
本文介绍了writeline所有特殊文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用console.writline方法在一个命令中显示Environment.GetFolderPath(Environment.SpecialFolder)的所有位置?



所以我希望它显示AdminTools,ApplicationData,CDBurning等的位置



它可行吗?

解决方案

< blockquote> 0)命令? c#中没有命令。有声明。方法调用是语句。



1)假设您只想要在一个语句中不想写所有枚举值。好吧,你没必要。

  foreach  var   value    Enum.GetValues( typeof (Environment.SpecialFolder)))
{
Console.WriteLine( {0}:{1},(Environment.SpecialFolder) value ,Environment.GetFolderPath((Environment.SpecialFolder)));
}



好​​的,这些至少是两个陈述。我将尝试将其合并为一个。

[更新]

仍然不是单语句版本,但只有一个 Console.WriteLine call:

 StringBuilder sb =  new  StringBuilder(); 
foreach var value Enum.GetValues( typeof (Environment.SpecialFolder)))
{
sb.Append( String .Format( {0} :{1} \ n,(Environment.SpecialFolder) value ,Environment.GetFolderPath((Environment.SpecialFolder))));
}
Console.WriteLine(sb.ToString());





[Update2]

嗯,仅供记录,这是一个单一陈述的解决方案:

 Console.WriteLine(
枚举 .GetValues( typeof (Environment.SpecialFolder))
.Cast< Environment.SpecialFolder>()
选择(x => x.ToString())
.Aggregate(
new StringBuilder(),(ag,n)=>
ag.Append( String .Format( {0}:{1} \ n,n,Environment.GetFolderPath((Environment.SpecialFolder)枚举 .Parse( typeof (Environment.SpecialFolder),n, true )) ))
).ToString());



第一个看起来更好:(


is it possible to use a console.writline method to show all of the locations of the Environment.GetFolderPath(Environment.SpecialFolder) in one command?

So i want it to show the location of AdminTools, ApplicationData, CDBurning etc

Is it doable??

解决方案

0) Command? There is no command in c#. There are statements. Method calls are statements.

1) Let's suppose you mean that you want only don't want to write all enumeration values each in one statements. Well, you don't have to.

foreach (var value in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
   Console.WriteLine("{0}: {1}", (Environment.SpecialFolder)value, Environment.GetFolderPath((Environment.SpecialFolder)value));
}


Ok, these are at least two statements. I will try to make it in one.
[Update]
Still not the one-statement version, but with one single Console.WriteLine call:

StringBuilder sb = new StringBuilder();
foreach (var value in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
   sb.Append(String.Format("{0}: {1}\n", (Environment.SpecialFolder)value, Environment.GetFolderPath((Environment.SpecialFolder)value)));
}
Console.WriteLine(sb.ToString());



[Update2]
Well, just for the record, here is a one-statement solution:

Console.WriteLine(
Enum.GetValues(typeof(Environment.SpecialFolder))
 .Cast<Environment.SpecialFolder>()
 .Select(x => x.ToString())
 .Aggregate(
    new StringBuilder(), (ag, n) =>
    ag.Append(String.Format("{0}: {1}\n",n, Environment.GetFolderPath((Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), n, true))))
).ToString());


The first one looks better :(


这篇关于writeline所有特殊文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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