private void PrintFilesInFolder(Folder parentFolder, DocumentService docSvc, double size)
        {
            File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
            if (files != null && files.Length > 0)
            {
                foreach (File file in files)
                {
                    if (file.FileSize >= size)
                    {
                        for (int vernum = file.VerNum; vernum >= 1; vernum--)
                        {
                            File verFile = docSvc.GetFileByVersion(file.MasterId, vernum);
                            Console.WriteLine(String.Format("{0,12:0,0}", verFile.FileSize) + " " + parentFolder.FullName + "/" + verFile.Name + " (Version " + vernum.ToString() + ")");
                        }
                    }
                }
            }

            Folder[] folders = docSvc.GetFoldersByParentId(parentFolder.Id, false);
            if (folders != null && folders.Length > 0)
            {
                foreach (Folder folder in folders)
                {
                    PrintFilesInFolder(folder, docSvc, size);
                }
            }
        }
        private void PrintFilesInFolder(Folder parentFolder, DocumentService docSvc, Int32 size, Int32 maxpath)
        {
            Document.File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
            if (files != null && files.Length > 0)
            {
                foreach (Document.File file in files)
                {
                    for (int vernum = file.VerNum; vernum >= 1; vernum--)
                    {
                        Document.File verFile = docSvc.GetFileByVersion(file.MasterId, vernum);
                        Int32 filepathlength = parentFolder.FullName.Length + verFile.Name.Length + size;
                        if (filepathlength >= maxpath)
                        {
                            Console.WriteLine(String.Format("{0,4:0,0}", filepathlength) + " chars: " + parentFolder.FullName + "/" + verFile.Name + " (Version " + vernum.ToString() + ")");
                        }
                    } 
                }
            }

            Folder[] folders = docSvc.GetFoldersByParentId(parentFolder.Id, false);
            if (folders != null && folders.Length > 0)
            {
                foreach (Folder folder in folders)
                {
                    PrintFilesInFolder(folder, docSvc, size, maxpath);
                }
            }
        }
        private void ProcessFilesInFolder(Folder parentFolder, DocumentService docSvc, string rootfolder, Boolean exportdwfs, string property, string propertyvalue, long propid)
        {
            VaultSyncReleased.Document.File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
            if (files != null && files.Length > 0)
            {
                foreach (VaultSyncReleased.Document.File file in files)
                {
                    Boolean matchesprop = false;
                    Console.WriteLine("Checking: " + parentFolder.FullName + "/" + file.Name);
                    Document.PropInst[] fileProperties = docSvc.GetProperties(new long[] { file.Id }, new long[] { propid });
                    if (fileProperties[0].Val != null)
                    {
                        //Console.WriteLine(" Property: " + property + " - " + fileProperties[0].Val.ToString());
                        if (fileProperties[0].Val.ToString() == propertyvalue)
                        {
                            matchesprop = true;
                        }
                    }
                    string outputfile = rootfolder + parentFolder.FullName.Substring(1) + "/" + file.Name;
                    outputfile = outputfile.Replace("/", "\\");
                    if (matchesprop)
                    {
                        int vernum = file.VerNum;
                        VaultSyncReleased.Document.File verFile = docSvc.GetFileByVersion(file.MasterId, vernum);
                        for (int counter = 0; counter < outputfile.Length; counter++)
                        {
                            if (outputfile.Substring(counter, 1) == "\\")
                            {
                                try
                                {
                                    System.IO.Directory.CreateDirectory(outputfile.Substring(0, counter));
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.ToString());
                                    Console.WriteLine(outputfile.Substring(0, counter));
                                }
                            }
                        }

                        try
                        {
                            if (System.IO.File.Exists(outputfile))
                            {
                                FileInfo fi = new FileInfo(outputfile);
                                if ((verFile.ModDate == fi.LastWriteTime) && (verFile.FileSize == fi.Length))
                                {
                                    Console.WriteLine("File is uptodate: " + outputfile);
                                }
                                else
                                {
                                    Console.WriteLine("Saving: " + outputfile);
                                    System.IO.File.Delete(outputfile); 
                                    DownloadFileInParts(verFile, docSvc, outputfile);
                                    FileInfo finew = new FileInfo(outputfile);
                                    finew.LastWriteTime = verFile.ModDate;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Saving: " + outputfile);
                                System.IO.File.Delete(outputfile); 
                                DownloadFileInParts(verFile, docSvc, outputfile);
                                FileInfo finew = new FileInfo(outputfile);
                                finew.LastWriteTime = verFile.ModDate;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ERROR: Saving " + outputfile);
                            Console.WriteLine(ex.Message.ToString());
                        }
                        Console.WriteLine("");
                        //Console.ReadLine();
                    }
                    else
                    {
                        if (System.IO.File.Exists(outputfile))
                        {
                            try
                            {
                                Console.WriteLine("Deleting: " + outputfile);
                                System.IO.File.Delete(outputfile);
                            }
                            catch
                            {
                                Console.WriteLine("ERROR: Deleting " + outputfile);
                            }
                        }
                        Console.WriteLine("");
                    }
                }
            }

            Folder[] folders = docSvc.GetFoldersByParentId(parentFolder.Id, false);
            if (folders != null && folders.Length > 0)
            {
                foreach (Folder folder in folders)
                {
                    ProcessFilesInFolder(folder, docSvc, rootfolder, exportdwfs, property, propertyvalue, propid);
                }
            }
        }
 public void RunCommand(string server, string vault, string username, string password, string file, string checkinfile, Boolean printerror, string comment)
 {
     SecurityService secSrv = new SecurityService();
     secSrv.SecurityHeaderValue = new VaultSingleFileCheckin.Security.SecurityHeader();
     secSrv.Url = "http://" + server + "/AutodeskDM/Services/SecurityService.asmx";
     try 
     {
         secSrv.SignIn(username, password, vault);
         DocumentService docSrv = new DocumentService();
         docSrv.SecurityHeaderValue = new VaultSingleFileCheckin.Document.SecurityHeader();
         docSrv.SecurityHeaderValue.UserId = secSrv.SecurityHeaderValue.UserId;
         docSrv.SecurityHeaderValue.Ticket = secSrv.SecurityHeaderValue.Ticket;
         docSrv.Url = "http://" + server + "/AutodeskDM/Services/DocumentService.asmx";
         Folder root = docSrv.GetFolderRoot();
         string filepath = System.IO.Path.GetDirectoryName(file);
         filepath = filepath.Replace("\\", "/");
         CreateFolder(docSrv, filepath);
         Folder filefolder = docSrv.GetFolderByPath(filepath);
         int fileflag = IsFileInFolder(filefolder, docSrv, file);
         //Console.WriteLine("File " + fileflag.ToString());
         byte[] bytes;
         switch (fileflag)
         {
             case 0:
                 Console.WriteLine("File is not in vault");
                 bytes = System.IO.File.ReadAllBytes(checkinfile);
                 File addedfile = docSrv.AddFile(filefolder.Id, System.IO.Path.GetFileName(checkinfile), comment,
                     System.IO.File.GetLastWriteTime(checkinfile), null, null, null, null, null,
                     FileClassification.None, false, bytes);
                 if (addedfile == null)
                 {
                     Console.WriteLine("ERROR: File not checked in to vault");
                 }
                 else
                 {
                     Console.WriteLine("File checked in to vault");
                 }
                 break;
             case 1:
                 Console.WriteLine("File is in vault (not checked out)");
                 File[] files = docSrv.GetLatestFilesByFolderId(filefolder.Id, true);
                 foreach (File afile in files)
                 {           
                     if (filefolder.FullName + "/" + afile.Name == file)
                     {
                         //docSrv.CheckoutFile(filefolder.Id, afile.MasterId,
                         //    Environment.MachineName, Environment.GetEnvironmentVariable("TEMP"), comment,
                         //    true, true, out bytes);
                         docSrv.CheckoutFile(filefolder.Id, afile.Id, CheckoutFileOptions.Master,
                                         Environment.MachineName, "c:\\", "Temporary Checkout",
                                         false, true, out bytes);
                         bytes = System.IO.File.ReadAllBytes(checkinfile);
                         File updatedfile = docSrv.CheckinFile(afile.MasterId, comment,
                             false, System.IO.File.GetLastWriteTime(checkinfile), null, null, null, null, null, false,
                             System.IO.Path.GetFileName(checkinfile), afile.FileClass, afile.Hidden, bytes);
                         if (updatedfile.Id  == afile.Id)
                         {
                             Console.WriteLine("ERROR: File not checked in to vault");
                         }
                         else
                         {
                             Console.WriteLine("File checked in to vault");
                         }
                     }
                 }
                 break;
             case 2:
                 Console.WriteLine("File is in vault (checked out to you)");
                 break;
             default:
                 Console.WriteLine("File is in vault (checked out to someone else)");
                 Console.WriteLine("Cannot check in file");
                 break;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error retrieving file");
         if (printerror)
         {
             Console.WriteLine(ex.ToString());
         }
         return;
     }
 }
        private int IsFileInFolder(Folder parentFolder, DocumentService docSvc, string filepath)
        {
            // Returns 0 if not found
            // Returns 1 if found & not checked out
            // Returns 2 if found & checked out to you
            // Returns 3 if found & checked out to someone else

            int flag = 0; 
            File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
            if (files != null && files.Length > 0)
            {
                foreach (File file in files)
                {
                    if (parentFolder.FullName + "/" + file.Name == filepath)
                    {
                        Console.WriteLine(filepath + " is in the vault.");
                        if (file.CheckedOut)
                        {
                            if (file.CkOutUserId == docSvc.SecurityHeaderValue.UserId)
                            {
                                flag = 2;
                            }
                            else
                            {
                                flag = 3;
                            }
                        }
                        else
                        {
                            flag = 1;
                        }
                    }
                }
            }
            return flag;
        }
 private void GetFilesInFolder(Folder parentFolder, DocumentService docSvc, string filepath, Int32 fileversion, string checkinfile)
 {
     File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
     if (files != null && files.Length > 0)
     {
         foreach (File file in files)
         {
             if (parentFolder.FullName + "/" + file.Name == filepath)
             {
                 for (int vernum = file.VerNum; vernum >= 1; vernum--)
                 {
                     File verFile = docSvc.GetFileByVersion(file.MasterId, vernum);
                     if (vernum == fileversion)
                     {
                         Console.WriteLine(String.Format("{0,12:0,0}", verFile.FileSize) + " " + parentFolder.FullName + "/" + verFile.Name + " (Version " + vernum.ToString() + ")");
                         Console.WriteLine("Writing to " + checkinfile);
                         byte[] bytes;
                         for (int counter = 0; counter < checkinfile.Length; counter++)
                         {
                             if (checkinfile.Substring(counter,1) == "\\")
                             {
                                 try 
                                 {
                                     System.IO.Directory.CreateDirectory(checkinfile.Substring(0,counter));
                                 }
                                 catch (Exception ex)
                                 {
                                     Console.WriteLine(ex.ToString());
                                     Console.WriteLine(checkinfile.Substring(0, counter));
                                 }
                             }
                         }
                         
                         string fileName = docSvc.DownloadFile(verFile.Id, true, out bytes);
                         System.IO.File.WriteAllBytes(checkinfile, bytes);
                     }
                 }
             }
         }
     }
 }
        private void ProcessFilesInFolder(Folder parentFolder, DocumentService docSvc, DocumentServiceExtensions docExSvc, string lifecycledef, string state, long lcid, long lcstate, Boolean force, string comment)
        {
            Autodesk.Connectivity.WebServices.File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
            if (files != null && files.Length > 0)
            {
                foreach (Autodesk.Connectivity.WebServices.File file in files)
                {
                    Console.WriteLine("");
                    Console.WriteLine(" " + parentFolder.FullName + "/" + file.Name);
                    Console.WriteLine("  Master ID   : " + String.Format("{0:0}", file.MasterId));
                    Console.WriteLine("  LifeCycle ID: " + file.FileLfCyc.LfCycDefId.ToString());
                    Console.WriteLine("      State   : " + file.FileLfCyc.LfCycStateName);
                    if (file.FileLfCyc.LfCycDefId != -1)
                    {
                        if (force) 
                            Console.WriteLine("  LifeCycle is already set: Forcing change");
                        else
                            Console.WriteLine("  LifeCycle is already set: Use -force to change");
                        
                    }
                    if ((file.FileLfCyc.LfCycDefId == -1) || (force))
                    {
                        try
                        {
                            docExSvc.UpdateFileLifeCycleDefinitions(new long[] { file.MasterId }, new long[] { lcid }, new long[] { lcstate }, comment);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ERROR: Changing LifeCycle " + parentFolder.FullName + "/" + file.Name + " (New LifeCycle - " + lifecycledef + ")");
                            Console.WriteLine(ex.Message.ToString());
                        }
                        finally 
                        {
                        }
                    }
#if DEBUG
                    Console.WriteLine("Press enter ...");
                    Console.ReadLine();
#endif
                }
            }

            Folder[] folders = docSvc.GetFoldersByParentId(parentFolder.Id, false);
            if (folders != null && folders.Length > 0)
            {
                foreach (Folder folder in folders)
                {
                    ProcessFilesInFolder(folder, docSvc, docExSvc, lifecycledef, state, lcid, lcstate, force, comment);
                }
            }
        }
        private void ProcessFilesInFolder(Folder parentFolder, DocumentService docSvc, DocumentServiceExtensions docextSrv, string statename, string state, long propid, long prophistid, string checkstates, long newstateid, long newlcdid, string newstate, long revertstateid, string revertstate, string lifecycledefinition)
        {
            string[] states = checkstates.Split('|');
            Autodesk.Connectivity.WebServices.File[] files = docSvc.GetLatestFilesByFolderId(parentFolder.Id, false);
            if (files != null && files.Length > 0)
            {
                foreach (Autodesk.Connectivity.WebServices.File file in files)
                {
                    Boolean matchesprop = false;
                    Console.WriteLine("");
                    Console.WriteLine("Checking: " + parentFolder.FullName + "/" + file.Name);
                    Autodesk.Connectivity.WebServices.PropInst[] fileProperties = docSvc.GetProperties(new long[] { file.Id }, new long[] { propid });
                    if (fileProperties[0].Val != null)
                    {
                        //Console.WriteLine(" Property: " + statename + " - " + fileProperties[0].Val.ToString());
                        if (fileProperties[0].Val.ToString() == state)
                        {
                            matchesprop = true;
                        }
                    }
                    //string outputfile = parentFolder.FullName.Substring(1) + "/" + file.Name;
                    //outputfile = outputfile.Replace("/", "\\");
                    if (matchesprop && (file.VerNum > states.Length))
                    {
                        Hashtable users = new Hashtable();
                        for (int vernum = file.VerNum; vernum > file.VerNum - states.Length; vernum--)
                        {
                            //Console.WriteLine(" Property Check: " + vernum.ToString());
                            //Console.WriteLine(" State Check: " + (file.VerNum - vernum).ToString());
                            Console.Write(" Checking State: " + states[file.VerNum - vernum]);
                            Autodesk.Connectivity.WebServices.File verFile = docSvc.GetFileByVersion(file.MasterId, vernum);
                            Autodesk.Connectivity.WebServices.PropInst[] verFileProperties = docSvc.GetProperties(new long[] { verFile.Id }, new long[] { propid });
                            if (verFileProperties[0].Val != null)
                            {
                                //Console.WriteLine(" Property: " + statename + " - " + verFileProperties[0].Val.ToString());
                                //Console.WriteLine(" Property: " + statename + " - " + verFile.CreateUserName);
                                if (fileProperties[0].Val.ToString() == state)
                                {
                                    users.Add(verFileProperties[0].Val.ToString(), verFile.CreateUserName);
                                    Console.Write(" : " + verFile.CreateUserName);
                                }
                            }
                            else
                            {
                                verFileProperties = docSvc.GetProperties(new long[] { verFile.Id }, new long[] { prophistid });
                                if (verFileProperties[0].Val != null)
                                {
                                    //Console.WriteLine(" Property: " + statename + " - " + verFileProperties[0].Val.ToString());
                                    //Console.WriteLine(" Property: " + statename + " - " + verFile.CreateUserName);
                                    if (fileProperties[0].Val.ToString() == state)
                                    {
                                        users.Add(verFileProperties[0].Val.ToString(), verFile.CreateUserName);
                                        Console.Write(" : " + verFile.CreateUserName);
                                    }
                                }
                            }
                            Console.WriteLine("");
                        }
                        Boolean statesmatch = true;
                        List<string> userlist = new List<string>();
                        foreach (string thisstate in states)
                        {
                            //Console.WriteLine("|" + thisstate + "|");
                            //Console.WriteLine("|" + users[thisstate].ToString() + "|");
                            if (!userlist.Contains(users[thisstate].ToString()))
                            {
                                userlist.Add(users[thisstate].ToString());
                            }
                            if (users[thisstate].ToString() == "")
                            {
                                statesmatch = false;
                            }
                        }
                        //Console.WriteLine(users.Count.ToString());
                        //Console.WriteLine(userlist.Count.ToString());
                        if (users.Count != userlist.Count)
                        {
                            statesmatch = false;
                        }
                        //Console.WriteLine(statesmatch.ToString());
                        if (statesmatch)
                        {
                            Console.WriteLine("");
                            Console.WriteLine(" " + parentFolder.FullName + "/" + file.Name);
                            //Console.WriteLine("  Master ID : " + String.Format("{0:0}", file.MasterId));
                            Console.WriteLine("  LifeCycle : " + lifecycledefinition);
                            Console.WriteLine("  State     : " + file.FileLfCyc.LfCycStateName);
                            Console.WriteLine("  New State : " + newstate);
                            try
                            {
                                docextSrv.UpdateFileLifeCycleStates(new long[] { file.MasterId }, new long[] { newstateid }, "Automatic State Change");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("ERROR: Changing state (New State - " + newstate + ")");
                                Console.WriteLine(ex.Message.ToString());
                            }
                            finally
                            {
                            }
                        }
                        else
                        {
                            if (revertstateid == -1)
                            {
                                Console.WriteLine("ERROR: Cannot change state, previous changes not performed by unique users");
                                Console.WriteLine("     : No revert state specified");
                            }
                            else
                            {
                                Console.WriteLine("");
                                Console.WriteLine(" " + parentFolder.FullName + "/" + file.Name);
                                //Console.WriteLine("  Master ID : " + String.Format("{0:0}", file.MasterId));
                                Console.WriteLine("  LifeCycle    : " + lifecycledefinition);
                                Console.WriteLine("  State        : " + file.FileLfCyc.LfCycStateName);
                                Console.WriteLine("  Reverting to : " + revertstate);
                                try
                                {
                                    docextSrv.UpdateFileLifeCycleStates(new long[] { file.MasterId }, new long[] { revertstateid }, "Automatic State Revert");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("ERROR: Changing state (New State - " + revertstate + ")");
                                    Console.WriteLine(ex.Message.ToString());
                                }
                                finally
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("");
                    }
                }
            }

            Folder[] folders = docSvc.GetFoldersByParentId(parentFolder.Id, false);
            if (folders != null && folders.Length > 0)
            {
                foreach (Folder folder in folders)
                {
                    ProcessFilesInFolder(folder, docSvc, docextSrv, statename, state, propid, prophistid, checkstates, newstateid, newlcdid, newstate, revertstateid, revertstate, lifecycledefinition);
                }
            }
        }