Exemple #1
0
        // **************************************************
        // **************************************************
        // **************************************************

        public static JsonData CreateDirectory
            (string preDefinedRootRelativePath, string path, string directoryName)
        {
            JsonData oJsonData = null;

            if (string.IsNullOrWhiteSpace(path))
            {
                string strErrorMessage =
                    "The path does not specify!";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(oJsonData);
            }

            if (string.IsNullOrWhiteSpace(directoryName))
            {
                string strErrorMessage =
                    "You did not specify directory name!";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(oJsonData);
            }

            directoryName = directoryName.Trim();

            path = FixPath(path);

            string strPreDefinedRootRelativePath =
                FixPreDefinedRootRelativePath(preDefinedRootRelativePath);

            string strRootRelativePath =
                string.Format("{0}{1}{2}",
                              strPreDefinedRootRelativePath, path, directoryName);

            string strPath =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativePath);

            if (System.IO.File.Exists(strPath))
            {
                string strErrorMessage =
                    "There is a file with this name!";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(oJsonData);
            }

            if (System.IO.Directory.Exists(strPath))
            {
                string strErrorMessage =
                    "This directory is already exist!";

                oJsonData =
                    new JsonData()
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(oJsonData);
            }

            try
            {
                System.IO.DirectoryInfo oDirectoryInfo =
                    System.IO.Directory.CreateDirectory(strPath);

                ViewModels.DirectoryViewModel directoryViewModel =
                    new ViewModels.DirectoryViewModel(oDirectoryInfo);

                string strInformationMessage =
                    "The directory created successfully.";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    Data           = directoryViewModel,
                    MessageText    = strInformationMessage,
                    State          = ViewModels.JsonResultStates.Success,
                };

                return(oJsonData);
            }
            catch
            {
                string strErrorMessage =
                    "Unexpected error when creating directory!";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(oJsonData);
            }
            finally
            {
            }
        }
Exemple #2
0
        public static JsonData GetDirectoriesAndFiles(string preDefinedRootRelativePath, string path)
        {
            JsonData jsonData = null;

            path = FixPath(path);

            preDefinedRootRelativePath =
                FixPreDefinedRootRelativePath(preDefinedRootRelativePath);

            string strRootRelativePath =
                string.Format("{0}{1}",
                              preDefinedRootRelativePath, path);

            string strPath =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativePath);

            // **************************************************
            while (strPath.Contains("\\\\"))
            {
                strPath =
                    strPath.Replace("\\\\", "\\");
            }

            string strRootPath =
                System.Web.HttpContext.Current.Server.MapPath("~");

            // Note: Never Write Below Code!
            //strRootPath =
            //	strRootPath.Substring(0, strRootPath.Length - 1);

            string[] strParts =
                strPath.Split('\\');

            string strTempPath = strParts[0];

            for (int intIndex = 1; intIndex <= strParts.Length - 1; intIndex++)
            {
                if (strParts[intIndex] == string.Empty)
                {
                    break;
                }

                if (strTempPath.StartsWith(strRootPath, System.StringComparison.InvariantCultureIgnoreCase))
                {
                    SetFullControlForEveryOne(path: strTempPath);
                }

                strTempPath += "\\" + strParts[intIndex];

                if (System.IO.Directory.Exists(strTempPath) == false)
                {
                    System.IO.Directory.CreateDirectory(strTempPath);
                }
            }
            // **************************************************

            // **************************************************
            ViewModels.PathAndDirectoriesAndFilesAndPathCollectionAndUrlViewModel oReturnViewModel =
                new ViewModels.PathAndDirectoriesAndFilesAndPathCollectionAndUrlViewModel
            {
                Path = path,
                PreDefinedRootRelativePath = preDefinedRootRelativePath,
            };
            // **************************************************

            // **************************************************
            ViewModels.PathItemViewModel pathItemViewModel = null;

            string[] pathCollection = path.Split('/');

            string fullPath = string.Empty;

            for (int index = 0; index <= pathCollection.Length - 2; index++)
            {
                string strCurrentPath =
                    pathCollection[index];

                fullPath =
                    string.Format("{0}{1}/",
                                  fullPath, strCurrentPath);

                if (strCurrentPath == string.Empty)
                {
                    // TODO
                    //strCurrentPath =
                    //	Resources.Captions.Root;
                }

                pathItemViewModel =
                    new ViewModels.PathItemViewModel
                {
                    Path = fullPath,
                    Name = strCurrentPath,
                };

                oReturnViewModel.PathCollection.Add(pathItemViewModel);
            }
            // **************************************************

            System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(strPath);

            foreach (System.IO.DirectoryInfo currentDirectoryInfo in directoryInfo.GetDirectories())
            {
                ViewModels.DirectoryViewModel directoryViewModel =
                    new ViewModels.DirectoryViewModel(currentDirectoryInfo);

                oReturnViewModel.Directories.Add(directoryViewModel);
            }

            foreach (System.IO.FileInfo currentFileInfo in directoryInfo.GetFiles())
            {
                ViewModels.FileViewModel fileViewModel =
                    new ViewModels.FileViewModel(currentFileInfo);

                oReturnViewModel.Files.Add(fileViewModel);
            }

            jsonData =
                new JsonData
            {
                Data  = oReturnViewModel,
                State = ViewModels.JsonResultStates.Success,
            };

            return(jsonData);
        }
Exemple #3
0
        public static JsonData Compress
            (string preDefinedRootRelativePath,
            ViewModels.PathAndDirectoriesAndFilesAndFileNameViewModel viewModel)
        {
            JsonData jsonData = null;

            // **************************************************
            if (string.IsNullOrWhiteSpace(viewModel.FileName))
            {
                string errorMessage =
                    "You did not specify file name!";

                jsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    MessageText    = errorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(jsonData);
            }

            viewModel.FileName = viewModel.FileName.Trim();
            // **************************************************

            viewModel.Path =
                FixPath(viewModel.Path);

            string strPreDefinedRootRelativePath =
                FixPreDefinedRootRelativePath(preDefinedRootRelativePath);

            string strRootRelativePath =
                string.Format("{0}{1}",
                              strPreDefinedRootRelativePath, viewModel.Path);

            string strPath =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativePath);

            string strRootRelativeCompressPath =
                string.Format("{0}{1}",
                              strRootRelativePath, viewModel.FileName);

            string strCompressPathName =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativeCompressPath);

            if (System.IO.File.Exists(strCompressPathName))
            {
                string strErrorMessage =
                    "There is a file with this name!";

                jsonData =
                    new JsonData()
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(jsonData);
            }

            if (System.IO.Directory.Exists(strCompressPathName))
            {
                string strErrorMessage =
                    "There is a directory with this name!";

                jsonData =
                    new JsonData()
                {
                    DisplayMessage = true,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };

                return(jsonData);
            }

            ViewModels.CreatedFileAndDirectoriesAndFilesViewModel
                returnViewModel = new ViewModels.CreatedFileAndDirectoriesAndFilesViewModel();

            Ionic.Zip.ZipFile zipFile = null;

            try
            {
                zipFile =
                    new Ionic.Zip.ZipFile(strCompressPathName);

                InitializeZipFile(zipFile);

                foreach (ViewModels.FileViewModel currentFileViewModel in viewModel.Files)
                {
                    string strCurrentPathName =
                        string.Format("{0}\\{1}",
                                      strPath, currentFileViewModel.Name);

                    if (System.IO.File.Exists(strCurrentPathName))
                    {
                        try
                        {
                            zipFile.AddFile
                                (fileName: strCurrentPathName,
                                directoryPathInArchive: string.Empty);

                            zipFile.Save();
                        }
                        catch (System.Exception ex)
                        {
                            System.IO.FileInfo oFileInfoThatHasError =
                                new System.IO.FileInfo(strCurrentPathName);

                            ViewModels.FileViewModel fileThatHasError =
                                new ViewModels.FileViewModel(oFileInfoThatHasError)
                            {
                                Message = ex.Message
                            };

                            returnViewModel.Files.Add(fileThatHasError);
                        }
                    }
                }

                foreach (ViewModels.DirectoryViewModel currentirectoryViewModel in viewModel.Directories)
                {
                    string strCurrentPath =
                        string.Format("{0}\\{1}",
                                      strPath, currentirectoryViewModel.Name);

                    if (System.IO.Directory.Exists(strCurrentPath))
                    {
                        try
                        {
                            zipFile.AddDirectory
                                (directoryName: strCurrentPath,
                                directoryPathInArchive: currentirectoryViewModel.Name);

                            zipFile.Save();
                        }
                        catch (System.Exception ex)
                        {
                            System.IO.DirectoryInfo oDirectoryInfoThatHasError =
                                new System.IO.DirectoryInfo(strCurrentPath);

                            ViewModels.DirectoryViewModel oDirectoryThatHasError =
                                new ViewModels.DirectoryViewModel(oDirectoryInfoThatHasError)
                            {
                                Message = ex.Message
                            };

                            returnViewModel.Directories.Add(oDirectoryThatHasError);
                        }
                    }
                }

                System.IO.FileInfo oFileInfo =
                    new System.IO.FileInfo(strCompressPathName);

                ViewModels.FileViewModel oFile =
                    new ViewModels.FileViewModel(oFileInfo);

                returnViewModel.CreatedFile = oFile;

                string strInformationMessage =
                    string.Format("The compressed file [{0}] created successfully.",
                                  viewModel.FileName);

                jsonData =
                    new JsonData
                {
                    Data        = returnViewModel,
                    MessageText = strInformationMessage,
                    State       = ViewModels.JsonResultStates.Success,
                };

                return(jsonData);
            }
            catch
            {
                string strErrorMessage =
                    "Unexpected Error on Creating Compressed File!";

                jsonData =
                    new JsonData
                {
                    MessageText = strErrorMessage,
                    State       = ViewModels.JsonResultStates.Error,
                };

                return(jsonData);
            }
            finally
            {
                if (zipFile != null)
                {
                    zipFile.Dispose();
                    zipFile = null;
                }
            }
        }
Exemple #4
0
        public static JsonData DeleteDirectoriesAndFiles
            (string preDefinedRootRelativePath, ViewModels.PathAndDirectoriesAndFilesViewModel viewModel)
        {
            JsonData oJsonData = null;

            viewModel.Path = FixPath(viewModel.Path);

            preDefinedRootRelativePath =
                FixPreDefinedRootRelativePath(preDefinedRootRelativePath);

            string strRootRelativePath =
                string.Format("{0}{1}",
                              preDefinedRootRelativePath, viewModel.Path);

            string strPath =
                System.Web.HttpContext.Current.Server.MapPath(strRootRelativePath);

            if (System.IO.Directory.Exists(strPath) == false)
            {
                oJsonData =
                    new JsonData
                {
                    State       = ViewModels.JsonResultStates.Error,
                    MessageText = "The current path is not valid!",
                };

                return(oJsonData);
            }

            ViewModels.DirectoriesAndFilesViewModel
                oReturnViewModel = new ViewModels.DirectoriesAndFilesViewModel();

            bool blnHasError = false;

            foreach (ViewModels.FileViewModel oFile in viewModel.Files)
            {
                string strCurrentPathName =
                    string.Format("{0}{1}", strPath, oFile.Name);

                if (System.IO.File.Exists(strCurrentPathName))
                {
                    try
                    {
                        System.IO.File.Delete(strCurrentPathName);
                    }
                    catch (System.Exception ex)
                    {
                        blnHasError = true;

                        System.IO.FileInfo oFileInfoThatHasError =
                            new System.IO.FileInfo(strCurrentPathName);

                        ViewModels.FileViewModel oFileThatHasError =
                            new ViewModels.FileViewModel(oFileInfoThatHasError);

                        if (ex.Message.Contains("it is being used by another process"))
                        {
                            oFileThatHasError.Message =
                                "It is being used by another process.";
                        }
                        else
                        {
                            oFileThatHasError.Message = ex.Message;
                        }

                        oReturnViewModel.Files.Add(oFileThatHasError);
                    }
                }
            }

            foreach (ViewModels.DirectoryViewModel oDirectory in viewModel.Directories)
            {
                string strCurrentPath =
                    string.Format("{0}{1}", strPath, oDirectory.Name);

                if (System.IO.Directory.Exists(strCurrentPath))
                {
                    try
                    {
                        System.IO.Directory.Delete(strCurrentPath, recursive: true);
                    }
                    catch (System.Exception ex)
                    {
                        blnHasError = true;

                        System.IO.DirectoryInfo oDirectoryInfoThatHasError =
                            new System.IO.DirectoryInfo(strCurrentPath);

                        ViewModels.DirectoryViewModel oDirectoryThatHasError =
                            new ViewModels.DirectoryViewModel(oDirectoryInfoThatHasError);

                        if (ex.Message.Contains("it is being used by another process"))
                        {
                            oDirectoryThatHasError.Message =
                                "It is being used by another process.";
                        }
                        else
                        {
                            oDirectoryThatHasError.Message = ex.Message;
                        }

                        oReturnViewModel.Directories.Add(oDirectoryThatHasError);
                    }
                }
            }

            if (blnHasError)
            {
                string strErrorMessage =
                    "Some selected directories and/or files does not deleted!";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    Data           = oReturnViewModel,
                    MessageText    = strErrorMessage,
                    State          = ViewModels.JsonResultStates.Error,
                };
            }
            else
            {
                string strInformationMessage =
                    "All selected directories and/or files deleted successfully.";

                oJsonData =
                    new JsonData
                {
                    DisplayMessage = true,
                    Data           = oReturnViewModel,
                    MessageText    = strInformationMessage,
                    State          = ViewModels.JsonResultStates.Success,
                };
            }

            return(oJsonData);
        }