使用程序将文件夹名字输出到文本文件中

目地:新建一个mvc的空项目,很多程序集都没有引用,想将默认的程序中的文件包含进新建项目中

  这需要使用nuget的命令,但是有的不知道其使用的名称,我就新建了一个默认的MVC工程,

  将新建工程的packages 文件夹下的包名称拿出来执行即可,可是使用的比较多,

反正是放假,写个简陋的不能再简陋的小工具获取下试试

using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;

namespace GetPackageName
{
    class Program
    { 
        static void Main(string[] args)
        { 
            //取得配置文件使用的配置新项目packages地址
            string path = ConfigurationManager.AppSettings["packagePath"] ?? @"C:\";
            //根据地址得到相应的目录
            DirectoryInfo dir = new DirectoryInfo(path);
            //简单的验证下,虽然添加了c:\默认地址
            if (dir.Exists)
            {
                //得到子文件夹
                var arrayDir = dir.GetDirectories();

                //得到输出文件地址文件流
                FileStream fs = new FileStream(ConfigurationManager.AppSettings["recordPath"]?? @"d:\packages.txt", FileMode.OpenOrCreate);
                //创建一个StreamWriter,用来想流中写入文件
                StreamWriter sw = new StreamWriter(fs);

                //对包进行正则匹配,去掉其版本信息
                Regex regex = new Regex(@"(^[a-zA-Z-.]*\.)");

                //循环子目录
                foreach (DirectoryInfo item in arrayDir)
                {
                    //匹配包的信息
                    var match = regex.Match(item.Name);
                    //将包名写入到流中
                    sw.Write(string.Format("install-package {0}",match.Groups[1].ToString().Trim('.') + System.Environment.NewLine));
                }
                //将流强制输出
                sw.Flush();
                //关闭写入流
                sw.Close();
                //关闭文件流
                fs.Close();
            }

        }
    }
}

配置文件配置为

  <appSettings>
    <add key ="packagePath" value ="C:\Users\xxxx\Documents\visual studio 2013\Projects\WebMVCPackage\packages"/>
    <add key ="recordPath" value="d:\packages.txt"/>
  </appSettings>

其他多的验证啥的都没写,随意耍耍而已

最后的结果 在d:\packages.txt 文件中得到

install-package Antlr
 install-package bootstrap
 install-package EntityFramework
 install-package EntityFramework.zh-Hans
 install-package jQuery
 install-package jQuery.Validation
 install-package Microsoft.AspNet.Identity.Core
 install-package Microsoft.AspNet.Identity.Core.zh-Hans
 install-package Microsoft.AspNet.Identity.EntityFramework
 install-package Microsoft.AspNet.Identity.EntityFramework.zh-Hans
 install-package Microsoft.AspNet.Identity.Owin
 install-package Microsoft.AspNet.Identity.Owin.zh-Hans
 install-package Microsoft.AspNet.Mvc
 install-package Microsoft.AspNet.Mvc.zh-Hans
 install-package Microsoft.AspNet.Razor
 install-package Microsoft.AspNet.Razor.zh-Hans
 install-package Microsoft.AspNet.Web.Optimization
 install-package Microsoft.AspNet.Web.Optimization.zh-Hans
 install-package Microsoft.AspNet.WebPages
 install-package Microsoft.AspNet.WebPages.zh-Hans
 install-package Microsoft.jQuery.Unobtrusive.Validation
 install-package Microsoft.Owin
 install-package Microsoft.Owin.Host.SystemWeb
 install-package Microsoft.Owin.Host.SystemWeb.zh-Hans
 install-package Microsoft.Owin.Security
 install-package Microsoft.Owin.Security.Cookies
 install-package Microsoft.Owin.Security.Facebook
 install-package Microsoft.Owin.Security.Facebook.zh-Hans
 install-package Microsoft.Owin.Security.Google
 install-package Microsoft.Owin.Security.Google.zh-Hans
 install-package Microsoft.Owin.Security.MicrosoftAccount
 install-package Microsoft.Owin.Security.MicrosoftAccount.zh-Hans
 install-package Microsoft.Owin.Security.OAuth
 install-package Microsoft.Owin.Security.Twitter
 install-package Microsoft.Owin.Security.Twitter.zh-Hans
 install-package Microsoft.Owin.Security.zh-Hans
 install-package Microsoft.Owin.zh-Hans
 install-package Microsoft.Web.Infrastructure
 install-package Modernizr
 install-package Newtonsoft.Json
 install-package Owin
 install-package Respond
 install-package WebGrease
View Code

 

posted on 2015-10-07 21:03  卍兲卐  阅读(494)  评论(0编辑  收藏  举报

导航