private void AddProjectExcuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (practicalFile.Projects.Count > 0)
            {
                practicalFile.Projects[practicalFile.Projects.Count - 1].IsLast = false;
            }

            PracticalProject project = new PracticalProject();

            project.IsLast = true;
            project.Index  = practicalFile.Projects.Count;
            practicalFile.Projects.Add(project);
        }
Example #2
0
        private void LoadPractical(string practicalFilePath)
        {
            FileStream fs = null;

            byte[]          buffer = null;
            PracticalFile[] items  = null;
            int[]           practicalProjectCountArray = null;
            int[][]         practicalProjectContentBufferLengthArray = null;
            int[][]         practicalProjectF1BufferLengthArray      = null;
            int[]           practicalProjectF2BufferLengthArray      = null;
            int[]           practicalNameBufferLength    = null;
            int[]           practicalAutographCount      = null;
            long[]          practicalContentBufferLength = null;

            try
            {
                fs     = new FileStream(practicalFilePath, FileMode.Open, FileAccess.Read);
                buffer = new byte[sizeof(int)];
                fs.Read(buffer, 0, buffer.Length);
                items = new PracticalFile[BitConverter.ToInt32(buffer, 0)];
                practicalProjectCountArray = new int[items.Length];
                practicalProjectContentBufferLengthArray = new int[items.Length][];
                practicalProjectF1BufferLengthArray      = new int[items.Length][];
                practicalProjectF2BufferLengthArray      = new int[items.Length];
                practicalNameBufferLength    = new int[items.Length];
                practicalAutographCount      = new int[items.Length];
                practicalContentBufferLength = new long[items.Length];

                for (int i = 0; i < items.Length; i++)
                {
                    items[i] = new PracticalFile();
                }

                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[sizeof(int)];
                    fs.Read(buffer, 0, buffer.Length);
                    practicalAutographCount[i] = BitConverter.ToInt32(buffer, 0);
                }

                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[sizeof(int)];
                    fs.Read(buffer, 0, buffer.Length);
                    practicalProjectCountArray[i] = BitConverter.ToInt32(buffer, 0);
                    practicalProjectContentBufferLengthArray[i] = new int[practicalProjectCountArray[i]];
                    practicalProjectF1BufferLengthArray[i]      = new int[practicalProjectCountArray[i]];
                }
                for (int i = 0; i < items.Length; i++)
                {
                    for (int j = 0; j < practicalProjectCountArray[i]; j++)
                    {
                        PracticalProject project = new PracticalProject();
                        project.Index = j;
                        items[i].Projects.Add(project);
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    for (int j = 0; j < practicalProjectCountArray[i]; j++)
                    {
                        buffer = new byte[sizeof(int)];
                        fs.Read(buffer, 0, buffer.Length);
                        practicalProjectContentBufferLengthArray[i][j] = BitConverter.ToInt32(buffer, 0);
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    for (int j = 0; j < practicalProjectCountArray[i]; j++)
                    {
                        buffer = new byte[sizeof(int)];
                        fs.Read(buffer, 0, buffer.Length);
                        practicalProjectF1BufferLengthArray[i][j] = BitConverter.ToInt32(buffer, 0);
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[sizeof(int)];
                    fs.Read(buffer, 0, buffer.Length);
                    practicalProjectF2BufferLengthArray[i] = BitConverter.ToInt32(buffer, 0);
                }
                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[sizeof(int)];
                    fs.Read(buffer, 0, buffer.Length);
                    practicalNameBufferLength[i] = BitConverter.ToInt32(buffer, 0);
                }
                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[sizeof(long)];
                    fs.Read(buffer, 0, buffer.Length);
                    practicalContentBufferLength[i] = BitConverter.ToInt64(buffer, 0);
                }
                for (int i = 0; i < items.Length; i++)
                {
                    for (int j = 0; j < practicalProjectCountArray[i]; j++)
                    {
                        buffer = new byte[practicalProjectContentBufferLengthArray[i][j]];
                        fs.Read(buffer, 0, buffer.Length);
                        items[i].Projects[j].Content = UTF8Encoding.UTF8.GetString(buffer);
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    for (int j = 0; j < practicalProjectCountArray[i]; j++)
                    {
                        buffer = new byte[practicalProjectF1BufferLengthArray[i][j]];
                        fs.Read(buffer, 0, buffer.Length);
                        items[i].Projects[j].HelperText = UTF8Encoding.UTF8.GetString(buffer);
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[practicalProjectF2BufferLengthArray[i]];
                    fs.Read(buffer, 0, buffer.Length);
                    items[i].HelperText = UTF8Encoding.UTF8.GetString(buffer);
                }
                for (int i = 0; i < items.Length; i++)
                {
                    for (int j = 0; j < practicalAutographCount[i]; j++)
                    {
                        Autograph autograph = new Autograph();
                        buffer = new byte[sizeof(int)];
                        fs.Read(buffer, 0, buffer.Length);
                        buffer = new byte[BitConverter.ToInt32(buffer, 0)];
                        fs.Read(buffer, 0, buffer.Length);
                        autograph.SheetName = UTF8Encoding.UTF8.GetString(buffer);
                        buffer = new byte[sizeof(int)];
                        fs.Read(buffer, 0, buffer.Length);
                        autograph.RowIndex = BitConverter.ToInt32(buffer, 0);
                        buffer             = new byte[sizeof(int)];
                        fs.Read(buffer, 0, buffer.Length);
                        autograph.ColIndex = BitConverter.ToInt32(buffer, 0);
                        items[i].Autographs.Add(autograph);
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    buffer = new byte[practicalNameBufferLength[i]];
                    fs.Read(buffer, 0, buffer.Length);
                    items[i].Index = i;
                    items[i].Name  = UTF8Encoding.UTF8.GetString(buffer);
                    items[i].Path  = System.IO.Path.Combine(loadPracticalFileFolder, items[i].Name + ".xls");

                    for (int j = 0; j < practicalProjectCountArray[i]; j++)
                    {
                        items[i].Projects[j].PracticalName = items[i].Name;
                    }
                }
                for (int i = 0; i < items.Length; i++)
                {
                    FileStream writeFs = null;
                    try
                    {
                        writeFs = new FileStream(items[i].Path, FileMode.Create, FileAccess.Write);
                        while (writeFs.Length != practicalContentBufferLength[i])
                        {
                            buffer = new byte[practicalContentBufferLength[i] - writeFs.Length > BUFFER_PACKAGE_LENGTH ? BUFFER_PACKAGE_LENGTH : practicalContentBufferLength[i] - writeFs.Length];
                            fs.Read(buffer, 0, buffer.Length);
                            writeFs.Write(buffer, 0, buffer.Length);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (writeFs != null)
                        {
                            writeFs.Dispose();
                        }
                    }
                }

                for (int i = 0; i < items.Length; i++)
                {
                    files.Add(items[i]);
                }
            }
            catch
            {
                throw new Exception("题库加载错误。");
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }
        }