Esempio n. 1
0
        public static FileMessage SaveToFile(T oItem, String path, String fileName)
        {
            if (String.IsNullOrWhiteSpace(fileName))
            {
                return(FileMessage.InvalidFileName);
            }
            if (String.IsNullOrWhiteSpace(path))
            {
                return(FileMessage.InvalidPath);
            }
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed)
                {
                    return(FileMessage.ImpersonationFailed);
                }
                else
                {
                    Boolean pathExists = Exists.Directory(path);
                    if (!pathExists)
                    {
                        pathExists = Create.Directory(path);
                    }
                    if (pathExists)
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(T));
                        StreamWriter  sw = new StreamWriter(path + "\\" + fileName);

                        xs.Serialize(sw, oItem);

                        sw.Flush();
                        sw.Close();
                        return(FileMessage.ChangeSaved);
                    }
                    else
                    {
                        return(FileMessage.DirectoryDoesntExist);
                    }
                }
            }
            catch (Exception ex)
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(FileMessage.Catch);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// if something fails, returns -1
        /// </summary>
        /// <param name="path"></param>
        /// <param name="recursive">Calculate Size of Subdirectories or not</param>
        /// <returns>-1 when impersonation fails, or catch</returns>
        public static Int64 Directory_Size(String path, Boolean recursive)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed)
                {
                    return(-1);
                }
                else
                {
                    Int64         size    = 0;
                    DirectoryInfo dirInfo = new DirectoryInfo(path);
                    if (Exists.Directory(dirInfo))
                    {
                        FileInfo[] fileInfos = dirInfo.GetFiles();
                        foreach (FileInfo file in fileInfos)
                        {
                            size += file.Length;
                        }
                        if (recursive)
                        {
                            DirectoryInfo[] directories = dirInfo.GetDirectories();
                            foreach (DirectoryInfo directory in directories)
                            {
                                size += Directory_Size(dirInfo.ToString() + directory.ToString() + "\\", true);
                            }
                        }
                        return(size);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(-1);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
            return(-1);
        }
Esempio n. 3
0
        /// <summary>
        /// "overwrite" is stronger than "append"
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        /// <param name="overwrite"></param>
        /// <param name="append">used only if overwrite is set to false</param>
        /// <returns></returns>
        public static FileMessage TextFile(String path, String content, Boolean overwrite, Boolean append)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed)
                {
                    return(FileMessage.ImpersonationFailed);
                }
                else
                {
                    if (overwrite || !Exists.File(path))
                    {
                        try
                        {
                            System.IO.File.WriteAllText(path, content);
                            return(FileMessage.FileCreated);
                        }
                        catch
                        { return(FileMessage.Catch); }
                    }
                    else
                    if (append)
                    {
                        try
                        {
                            System.IO.File.AppendAllText(path, content);
                            return(FileMessage.FileCreated);
                        }
                        catch
                        { return(FileMessage.Catch); }
                    }
                    return(FileMessage.FileExist);
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(FileMessage.Catch);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// The BMP stream is loaded in mbpStream parameter
        /// </summary>
        /// <param name="path"></param>
        /// <param name="bmpStream"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap ImageStream(String path, ref FileMessage fMessage)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed)
                {
                    fMessage = FileMessage.ImpersonationFailed;
                    return(null);
                }
                else
                {
                    FileMessage exists = Exists.File_FM(path);
                    if (exists == FileMessage.FileExist)
                    {
                        System.Drawing.Bitmap bmpStream = null;
                        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)){
                            bmpStream = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(fs));
                            fMessage  = FileMessage.Read;
                            fs.Close();
                        }


                        return(bmpStream);
                    }
                    else
                    {
                        fMessage = exists;
                        return(null);
                    }
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                fMessage = FileMessage.Catch;
                return(null);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Esempio n. 5
0
        public static FileMessage ImageSize(String path, ref Int32 height, ref Int32 width)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed)
                {
                    return(FileMessage.ImpersonationFailed);
                }
                else
                {
                    FileMessage exists = Exists.File_FM(path);
                    if (exists == FileMessage.FileExist)
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)){
                            System.Drawing.Image objImage = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(fs));
                            width  = objImage.Width;
                            height = objImage.Height;
                            objImage.Dispose();
                            fs.Close();
                        }
                        return(FileMessage.FileExist);
                    }
                    else
                    {
                        return(exists);
                    }
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(FileMessage.Catch);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// If something fails, it returns true
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Boolean Directory_isEmpty(String path)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated)
                {
                    oImpersonate.ImpersonateValidUser();
                }
                if (Exists.Directory(path))
                {
                    DirectoryInfo directory = new DirectoryInfo(path);
                    return(directory.GetFiles().Count() == 0 && directory.GetDirectories().Count() == 0);
                }
                else
                {
                    return(true);
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(true);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Esempio n. 7
0
        public static String TextFile(String filename)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated)
                {
                    oImpersonate.ImpersonateValidUser();
                }
                if (Exists.File(filename))
                {
                    return(System.IO.File.ReadAllText(filename));
                }
                else
                {
                    return(String.Empty);
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(String.Empty);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Esempio n. 8
0
        public static FileMessage Directory_FM(String path)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && (oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed))
                {
                    return(FileMessage.ImpersonationFailed);
                }
                else
                {
                    if (Exists.Directory(path))
                    {
                        return(FileMessage.DirectoryExist);
                    }
                    else
                    {
                        String ShareName = string.Empty;
                        if (path.Contains("/"))
                        {
                            path = path.Replace(@"/", @"\");
                        }
                        if (path.StartsWith("\\"))
                        {
                            ShareName = path.Substring(0, path.IndexOf(@"\", 2));
                            path      = path.Replace(ShareName, "");
                        }
                        if (path == "")
                        {
                            return(FileMessage.DirectoryExist);
                        }
                        else
                        {
                            String TempPath = ShareName;
                            foreach (string oDirectoryPath in path.Split(System.IO.Path.PathSeparator)) //sarebbe meglio aggiungendo il parametro StringSplitOptions.RemoveEmptyEntries e togliendo l'if seguente, ma il compilatore ha deciso di non accettarlo.
                            {
                                TempPath += oDirectoryPath + @"\";
                                if (!Exists.Directory(TempPath))
                                {
                                    try
                                    {
                                        System.IO.Directory.CreateDirectory(TempPath);
                                        return(FileMessage.FolderCreated);
                                    }
                                    catch
                                    {
                                        return(FileMessage.NotCreated);
                                    }
                                }
                                else
                                {
                                    return(FileMessage.DirectoryExist);
                                }
                            }
                        }
                    }
                    return(FileMessage.None);
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(FileMessage.Catch);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }