/// <summary>
        /// Gets the path of the data file directory used by <see cref="FileDataProvider"/>s
        /// managed by <c>this</c>, relative to the base uri of the <see cref="Presentation"/>
        /// owning the file data provider manager.
        /// </summary>
        /// <returns>The path</returns>
        /// <remarks>
        /// The DataFileDirectory is initialized lazily:
        /// If the DataFileDirectory has not been explicitly initialized using the <see cref="DataFileDirectory"/> setter,
        /// retrieving <see cref="DataFileDirectory"/> will assing it the default value "Data"</remarks>

        ////{
        ////    get
        ////    {
        ////        if (mDataFileDirectory == null) mDataFileDirectory = "Data";
        ////        return mDataFileDirectory;
        ////    }
        ////set
        ////{
        ////    if (value == null)
        ////    {
        ////        throw new exception.MethodParameterIsNullException(
        ////            "The DataFileDirectory can not be null");
        ////    }
        ////    if (mDataFileDirectory != null)
        ////    {
        ////        throw new exception.IsAlreadyInitializedException(
        ////            "The DataProviderManager has already been initialized with a DataFileDirectory");
        ////    }
        ////    Uri tmp;
        ////    if (!Uri.TryCreate(value, UriKind.Relative, out tmp))
        ////    {
        ////        throw new exception.InvalidUriException(String.Format(
        ////                                                    "DataFileDirectory must be a relative Uri, '{0}' is not",
        ////                                                    value));
        ////    }

        ////    if (!Directory.Exists(value))
        ////    {
        ////        Directory.CreateDirectory(value);
        ////    }
        ////    mDataFileDirectory = value;
        ////}
        ////}

        ///// <summary>
        ///// Moves the data file directory of the manager
        ///// </summary>
        ///// <param name="newDataFileDir">The new data file direcotry</param>
        ///// <param name="deleteSource">A <see cref="bool"/> indicating if the source/old data files shlould be deleted</param>
        ///// <param name="overwriteDestDir">A <see cref="bool"/> indicating if the new data directory should be overwritten</param>
        //public void MoveDataFiles(string newDataFileDir, bool deleteSource, bool overwriteDestDir)
        //{
        //    if (Path.IsPathRooted(newDataFileDir))
        //    {
        //        throw new exception.MethodParameterIsOutOfBoundsException(
        //            "The data file directory path must be relative");
        //    }
        //    string oldPath = DataFileDirectoryFullPath;
        //    mDataFileDirectory = newDataFileDir;
        //    string newPath = DataFileDirectoryFullPath;
        //    try
        //    {
        //        if (Directory.Exists(newPath))
        //        {
        //            if (overwriteDestDir)
        //            {
        //                Directory.Delete(newPath);
        //            }
        //            else
        //            {
        //                throw new exception.OperationNotValidException(
        //                    String.Format("Directory {0} already exists", newPath));
        //            }
        //        }
        //        CopyDataFiles(oldPath, newPath);
        //        if (deleteSource && Directory.Exists(oldPath))
        //        {
        //            Directory.Delete(oldPath);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        throw new exception.OperationNotValidException(
        //            String.Format("Could not move data files to {0}: {1}", newPath, e.Message),
        //            e);
        //    }
        //}

        private string getDataFileDirectoryFullPath(Uri baseUri)
        {
            if (!baseUri.IsFile)
            {
                throw new exception.InvalidUriException(
                          "The base Uri of the presentation to which the DataProviderManager belongs must be a file Uri");
            }

            string localPathPresentation = baseUri.LocalPath;

            if (File.Exists(localPathPresentation))
            {
                localPathPresentation = Path.GetDirectoryName(localPathPresentation);
            }

            string localPath = Path.Combine(localPathPresentation, DataFileDirectory);

            //Uri dataFileDirUri = new Uri(baseUri, DataFileDirectory);
            //string localPath = dataFileDirUri.LocalPath;

            if (!Directory.Exists(localPath))
            {
                FileDataProvider.CreateDirectory(localPath);
            }

            return(localPath);
        }
Ejemplo n.º 2
0
        public static void MoveDirectory(string sourcePath, string destPath)
        {
            if (!Directory.Exists(destPath))
            {
                FileDataProvider.CreateDirectory(destPath);
            }

            foreach (string file in Directory.GetFiles(sourcePath))
            {
                string dest = Path.Combine(destPath, Path.GetFileName(file));
                File.Move(file, dest);
                try
                {
                    File.SetAttributes(dest, FileAttributes.Normal);
                }
                catch
                {
                }
            }

            foreach (string folder in Directory.GetDirectories(sourcePath))
            {
                string dest = Path.Combine(destPath, Path.GetFileName(folder));
                FileDataProvider.MoveDirectory(folder, dest);
            }

            FileDataProvider.DeleteDirectory(sourcePath);
        }
Ejemplo n.º 3
0
        public void CopyFileDataProvidersToDataFolder(string fullDataFolderPath)
        {
            if (!Directory.Exists(fullDataFolderPath))
            {
                FileDataProvider.CreateDirectory(fullDataFolderPath);
            }

            reportProgress(-1, Path.GetFileName(fullDataFolderPath));

            int       index        = 0;
            const int progressStep = 10;
            int       progress     = progressStep;

            List <FileDataProvider> list = new List <FileDataProvider>(m_Presentation.DataProviderManager.ManagedFileDataProviders);

            foreach (FileDataProvider fdp in list)
            {
                index++;
                progress = 100 * index / list.Count;

                reportProgress_Throttle(progress, string.Format("{1}/{2} ({0})", fdp.DataFileRelativePath, index, list.Count));


                if (RequestCancellation)
                {
                    return;
                }

                string pathSource = Path.Combine(m_Presentation.DataProviderManager.DataFileDirectoryFullPath, fdp.DataFileRelativePath);
                if (!File.Exists(pathSource))
                {
                    throw new exception.DataMissingException(String.Format("File does not exist: {0}", pathSource));
                }
                string pathDest = Path.Combine(fullDataFolderPath, fdp.DataFileRelativePath);
                if (!File.Exists(pathDest))
                {
                    File.Copy(pathSource, pathDest);
                    try
                    {
                        File.SetAttributes(pathDest, FileAttributes.Normal);
                    }
                    catch
                    {
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Writes data from data provider stream to a file external to project
        /// </summary>
        /// <param name="exportFilePath"></param>
        /// <param name="canOverwrite"></param>
        public virtual void ExportDataStreamToFile(string exportFilePath, bool canOverwrite)
        {
            if (exportFilePath == null)
            {
                throw new exception.MethodParameterIsNullException("external file path cannot be null");
            }

            if (File.Exists(exportFilePath) && !canOverwrite)
            {
                throw new System.Exception("Export file with same name already exists");
            }

            Stream source = OpenInputStream();

            if (source.Length == 0)
            {
                source.Close();
                throw new exception.InputStreamIsTooShortException("The data provider has no data to export to external file");
            }

            const uint BUFFER_SIZE = 1024 * 1024; // 1 MB MAX BUFFER

            string parentdir = Path.GetDirectoryName(exportFilePath);

            if (!Directory.Exists(parentdir))
            {
                FileDataProvider.CreateDirectory(parentdir);
            }

            FileStream exportFileStream = null;

            try
            {
                //exportFileStream = File.Create(exportFilePath);
                exportFileStream = new FileStream(exportFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
            }
            catch (Exception ex)
            {
                source.Close();

#if DEBUG
                Debugger.Break();
#endif
                throw;
            }

            try
            {
                StreamUtils.Copy(source, 0, exportFileStream, BUFFER_SIZE);

                //if (source.Length <= BUFFER_SIZE)
                //{
                //    byte[] buffer = new byte[source.Length];
                //    int read = source.Read(buffer, 0, (int)source.Length);
                //    exportFileStream.Write(buffer, 0, read);
                //}
                //else
                //{
                //    byte[] buffer = new byte[BUFFER_SIZE];
                //    int bytesRead = 0;
                //    while ((bytesRead = source.Read(buffer, 0, BUFFER_SIZE)) > 0)
                //    {
                //        exportFileStream.Write(buffer, 0, bytesRead);
                //    }

                //}
            }
            finally
            {
                exportFileStream.Close();
                source.Close();
            }
        }