コード例 #1
0
        private void saveFileList(string xmlFileList, string guid)
        {
            System.Data.DataSet      ds        = new System.Data.DataSet();
            System.IO.StringReader   reader    = new System.IO.StringReader(xmlFileList);
            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(reader);
            ds.ReadXml(xmlReader);

            AssignmentM assign    = AssignmentM.Load(_assignmentID);
            string      targetdir = SharedSupport.AddBackSlashToDirectory(assign.StorageDirectory + _userID);
            string      uploadDir = SharedSupport.AddBackSlashToDirectory(System.Web.HttpContext.Current.Request.MapPath(String.Empty, Constants.ASSIGNMENTMANAGER_UPLOAD_DIRECTORY, true)) +
                                    SharedSupport.AddBackSlashToDirectory(guid);

            try
            {
                if (Directory.Exists(targetdir))
                {
                    // If the directory exists, remove it so we are not left with extra files around.
                    Directory.Delete(targetdir, true);
                }
                Directory.CreateDirectory(targetdir);
            }
            catch
            {
            }

            //get max project size from settings table (in megabytes)
            Int64 maxProjectSize = new Int64();

            maxProjectSize = Convert.ToInt32(SharedSupport.GetSetting(Constants.MAX_PROJECT_SETTING)) * Constants.BytesInMegaByte;
            //create variable to track project size
            Int64 currentProjectSize = new Int64();

            currentProjectSize = 0;

            //Cycle through all uploaded files and save a record for each in the user assignment files table
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string filename = ds.Tables[0].Rows[i]["Filename"].ToString();
                //make sure the file name is not blank
                if (filename != null && filename != String.Empty)
                {
                    filename = filename.Replace("/", @"\");
                    if (filename.StartsWith(@"\"))
                    {
                        filename = filename.Remove(0, 1);
                    }
                    this.AddFile(filename);
                }
                else
                {
                    //Throw an error because the file name for the given record is blank
                    throw new ApplicationException(SharedSupport.GetLocalizedString("StudentAssignment_BlankFileName_Error"));
                }

                string uploadFilePath = uploadDir + filename;
                string targetFilePath = targetdir + filename;
                string fullTargetDir  = targetFilePath.Substring(0, targetFilePath.LastIndexOf(@"\"));
                if (!Directory.Exists(fullTargetDir))
                {
                    Directory.CreateDirectory(fullTargetDir);
                }
                if (!File.Exists(uploadFilePath))
                {
                    throw new ApplicationException(SharedSupport.GetLocalizedString("SharedSupport_InvalidFileLocation_Error"));
                }
                else
                {
                    //Get the size of the file and add it to other's to see if project exceeds
                    //    the maximum size limit held in the settings table
                    currentProjectSize += new FileInfo(uploadFilePath).Length;

                    if (currentProjectSize > maxProjectSize)
                    {
                        //delete all files
                        Directory.Delete(uploadDir, true);
                        Directory.Delete(targetdir, true);
                        throw new ApplicationException(SharedSupport.GetLocalizedString("StudentAssignment_ProjectTooLarge") + maxProjectSize.ToString() + SharedSupport.GetLocalizedString("StudentAssignment_Megabytes"));
                    }
                }
                File.Copy(uploadFilePath, targetFilePath, true);
            }
        }