private bool DeleteMyFile(int FileID, int EventID)
        {
            bool result             = true;
            ProjectEventService pes = new ProjectEventService();
            string dir     = ConfigurationManager.AppSettings["PhysicalPath"];
            string virpath = pes.GetProjectEventDocument(FileID).PhysicalVirtualPath;

            System.IO.File.Delete(dir + virpath);
            pes.DeleteDocument(FileID, EventID);
            return(result);
        }
        public string WordAutoSave(int FileID, int EventID)
        {
            //depending on .ext it is either doc or excel.
            bool result             = true;
            ProjectEventService pes = new ProjectEventService();
            string dir     = ConfigurationManager.AppSettings["PhysicalPath"];
            string virpath = dir + pes.GetProjectEventDocument(FileID).PhysicalVirtualPath;

            try
            {
                object           filename    = virpath; //@"\\192.168.33.17\Download\IT\TEST.docx";
                Word.Application application = new Word.Application();
                application.Visible = true;
                //  int hello = application.Documents.Count();
                Word.Document document = application.Documents.Open(filename);
            }
            catch (Exception e)
            {
                return("Exception caught. " + e);
            }


            return(virpath);
        }
        public ActionResult UserAction(string useraction, ProjectEvent o, string SSPOrGrantee)
        {
            if (Session["ProposalID"] == null)
            {
                return(RedirectToAction("Search", "ProposalInfo"));
            }

            ProjectEventService pes = new ProjectEventService();

            switch (useraction.ToLower())
            {
            case "generate file from template":
                if (o.EventID > 0)
                {
                    pes.Update(o);
                }
                return(RedirectToAction("SelectTemplate", new { ProjectID = o.ProjectID, EventID = o.EventID, EventTypeID = o.EventTypeID, ReportPeriodID = o.ReportPeriodID }));

            case "insert":
                // if (o.EventID > 0)
                pes.Insert(o);
                break;
                return(RedirectToAction("SelectTemplate", new { ProjectID = o.ProjectID, EventID = o.EventID, o.ReportPeriodID }));

            case "update":
                if (o.EventID > 0)       //there must be eventID and user must be logged in.
                {
                    pes.Update(o);
                    if (Request.Files.Count != 0)
                    {
                        for (int i = 0; i < Request.Files.Count; i++)
                        {
                            if (!String.IsNullOrEmpty(Request.Files[i].FileName))
                            {
                                string FileKey;
                                FileKey = Request.Files.GetKey(i);
                                string FileID;
                                FileID = FileKey.Replace("files_", "");

                                string dir = ConfigurationManager.AppSettings["PhysicalPath"];

                                //string MyString="";
                                //int FileLen;
                                //System.IO.Stream MyStream;

                                //FileLen = Request.Files[i].ContentLength;
                                //byte[] input = new byte[FileLen];

                                //// Initialize the stream.
                                //MyStream = Request.Files[i].InputStream;

                                //// Read the file into the byte array.
                                //MyStream.Read(input, 0, FileLen);

                                //// Copy the byte array into a string.
                                //for (int Loop1 = 0; Loop1 < FileLen; Loop1++)
                                //    MyString = MyString + input[Loop1].ToString();
                                string fileOriginalName = Request.Files[i].FileName;
                                string ExtenSion        = Path.GetExtension(fileOriginalName);

                                if (FileID == "0")
                                {
                                    #region InsertNewDocument


                                    ProjectEventDocument myDoc = new ProjectEventDocument();
                                    string virtdir             = "files/" + o.ProjectID.ToString() + "/";//"files/A" + DateTime.Now.Year.ToString().Substring(2) + o.ProjectID.ToString() + "/";
                                    dir += virtdir.Replace("/", "\\");
                                    if (!(Directory.Exists(dir)))
                                    {
                                        Directory.CreateDirectory(dir);
                                    }
                                    string fileformat = o.EventID.ToString() + "_" + o.ProjectID.ToString() + "_";
                                    int    fi;
                                    //checking other file names to come up with new file number.
                                    for (fi = 1; System.IO.File.Exists(dir + fileformat + fi.ToString() + ExtenSion); fi++)
                                    {
                                    }



                                    Request.Files[i].SaveAs(dir + fileformat + fi.ToString() + ExtenSion);

                                    myDoc.Author               = session.CurrentUser.FirstName + " " + session.CurrentUser.LastName + " " + session.CurrentUser.MiddleName;
                                    myDoc.CreatedDate          = DateTime.Now;
                                    myDoc.fileextension        = ExtenSion.Replace(".", ""); //needs "doc"
                                    myDoc.FileName             = fileformat + fi.ToString() + ExtenSion;
                                    myDoc.PhysicalAbsolutePath = dir + fileformat + fi.ToString() + ExtenSion;
                                    myDoc.PhysicalVirtualPath  = "\\" + virtdir.Replace("/", "\\") + fileformat + fi.ToString() + ExtenSion;
                                    myDoc.UpdatedDate          = myDoc.CreatedDate;
                                    myDoc.WebVirtualPath       = "/" + virtdir + fileformat + fi.ToString() + ExtenSion;
                                    pes.InsertDocument(myDoc, o.EventID);
                                    #endregion
                                }
                                else
                                {
                                    #region Update Document      //with different file extension it won't work.
                                    //get object by fileid
                                    //update object

                                    ProjectEventDocument myDoc = pes.GetProjectEventDocument(int.Parse(FileID));
                                    //System.IO.File.WriteAllText(dir + myDoc.PhysicalVirtualPath, MyString);

                                    if (myDoc.fileextension == ExtenSion.Replace(".", ""))      //if extensions are the same then Update allowed.
                                    {
                                        Request.Files[i].SaveAs(dir + myDoc.PhysicalVirtualPath);
                                        myDoc.Author      = session.CurrentUser.FirstName + " " + session.CurrentUser.LastName + " " + session.CurrentUser.MiddleName;;
                                        myDoc.UpdatedDate = DateTime.Now;
                                        pes.UpdateProjectEventDocument(myDoc);
                                    }
                                    //get object by fileid
                                    //update object
                                    #endregion
                                }
                            }
                        }
                    }
                }
                break;

            case "delete":
                string mydir = Request.MapPath("~");
                foreach (var item in o.ProjectEventDocs)
                {
                    try
                    {
                        System.IO.File.Delete(mydir + item.ProjectEventDocument.PhysicalVirtualPath);
                    }
                    catch (Exception ex) { }
                    DeleteMyFile(item.ProjectEventDocumentID, o.EventID);
                }
                pes.Delete(o.EventID);
                break;
            }

            return(RedirectToAction("Index", new { id = o.EventID }));
        }