using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System.Data;
using System.Collections;


namespace ResetOneWebDocumentsLibraryVersionNum
{
    class Program
    {
        static void Main(string[] args)
        {
           
            string[] temp = System.IO.File.ReadAllLines("path.txt", System.Text.Encoding.UTF8);
            //System.Text.Encoding.GetEncoding("unicode"));

            InitionFile();
           
            Console.WriteLine("Please input Version num which had been set:"); //对当前历史版本数设置超过多少的文档库进行重置
            String getVerNumString = Console.ReadLine();
            Int16 getVerNum = Convert.ToInt16(getVerNumString);
            Console.WriteLine("Please input Version num which will be used:");//输入重新设置文档库的历史版本值
            String resetVerNumString = Console.ReadLine();
            Int16 resetVerNum = Convert.ToInt16(resetVerNumString);


            // Uri webAppUri = new Uri(weburl);
            // SPWebApplication webApplication = SPWebApplication.Lookup(webAppUri);

            Console.WriteLine("WebURL\tDocumentUrl\tDocumentLibrary\tMajorVersionLimit\tMinorVersionsLimit");
            //writer.WriteLine("WebURL\tDocumentUrl\tDocumentLibrary\tMajorVersionLimit\tMinorVersionsLimit");
            foreach (string weburl in temp)
            {
            using (SPSite site = new SPSite(weburl))
            {
                using (SPWeb web = site.OpenWeb())
                {
            
                foreach (SPList list in web.Lists)
                {
                   
                    
                    if (list.EnableVersioning.ToString() == "True"  && list.BaseTemplate.ToString() == "DocumentLibrary" && list.Hidden.ToString() != "True" && list.EnableVersioning.ToString() == "True" && list.Title != "Site Collection Documents"  && list.Title != "Site Collection Images" && list.Title != "Style Library")
                        {
                            
                           if (list.MajorVersionLimit == 0 || list.MajorVersionLimit >= getVerNum || list.MajorWithMinorVersionsLimit >= getVerNum)
                            {
                                if ( list.MajorVersionLimit >= getVerNum) list.MajorVersionLimit = 20;

                                if (list.EnableMinorVersions.ToString() != "False" && list.MajorWithMinorVersionsLimit >= getVerNum) list.MajorWithMinorVersionsLimit = resetVerNum;
                                list.Update();
                                
                                Console.WriteLine(list.Title + "\t" + list.MajorVersionLimit + "\t" + list.MajorWithMinorVersionsLimit);

                                writer.WriteLine(list.ParentWebUrl.ToString() + "\t" + list.DefaultViewUrl + "\t" + list.Title + "\t" + list.MajorVersionLimit + "\t" + list.MajorWithMinorVersionsLimit);

                            }
                       
                        }
                     
                    
                }
            }
        }
        }

            CloseFile();
           // Console.ReadKey();
        }

        static StreamWriter writer;
        static void InitionFile()
        {
            writer = new StreamWriter("HistoryVer.txt", true);
        }
        static void CloseFile()
        {
            writer.Flush();
            writer.Close();
        }

        static void WriteLine(string msg)
        {
            writer.WriteLine(msg);
        }
    }
}