public static FileMessage UploadFile(ref Telerik.Web.UI.UploadedFile file, String filePath) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { file.SaveAs(filePath); return(FileMessage.FileCreated); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <summary> /// if destination exists, files will not be overwritten /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public static FileMessage CopyFile_FM(String source, String destination) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { string destinationPath = destination.Substring(0, destination.Length - Helper.StringReverse(destination).IndexOf(@"\")); Directory(destinationPath); System.IO.File.Copy(source, destination); return(FileMessage.FileCreated); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static Int64 File_Size(String path) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(-1); } else { return(new FileInfo(path).Length); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(-1); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <summary> /// In case of error, returns DateTime.MinValue /// </summary> /// <param name="path"></param> /// <returns></returns> public static DateTime File_LastWrite(String path) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(DateTime.MinValue); } else { return(System.IO.File.GetLastWriteTime(path)); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(DateTime.MinValue); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static dtoFileSystemInfo File_dtoInfo(String path) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated) { oImpersonate.ImpersonateValidUser(); } FileInfo info = new System.IO.FileInfo(path); //viene creato anche se non ha accesso al file, o se il file non esiste return(new dtoFileSystemInfo() { CreationTime = info.CreationTime, Exists = info.Exists, Extension = info.Extension, FullName = info.FullName, IsReadOnly = info.IsReadOnly, Length = info.Length, Name = info.Name }); } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(null); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static FileInfo File_Info(String path) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated) { oImpersonate.ImpersonateValidUser(); } return(new System.IO.FileInfo(path)); //viene creato anche se non ha accesso al file, o se il file non esiste } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(null); //new System.IO.FileInfo(path); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static System.IO.FileStream FileStream(String fileName) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(null); } else { return(new System.IO.FileStream(fileName, System.IO.FileMode.Create)); } } catch (Exception ex) { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(null); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <summary> /// Unused-Untested after impersonation implementation /// </summary> /// <param name="fullname">File name</param> /// <param name="lines">number of lines to retrieve</param> /// <returns></returns> public static CsvFile LoadCsvFile(String fullname, dtoCsvSettings settings, int lines) { CsvFile result = null; Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated) { oImpersonate.ImpersonateValidUser(); } CsvFileReader reader = new CsvFileReader(); result = reader.ReadFile(fullname, settings, lines); } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } result = null; } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } return(result); }
public static FileMessage Image(System.Drawing.Image image, String path, System.Drawing.Imaging.ImageFormat imageFormat) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { image.Save(path, imageFormat); return(FileMessage.FileCreated); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static FileMessage ZipFilesRename(List <dtoFileToZip> zipFiles, String OutputFile) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { //ToDo: zip files... return(FileMessage.None); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
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(); } } }
public static bool DriveFreeBytes(string folderName, out ulong freespace) { freespace = 0; Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(false); } else { if (string.IsNullOrEmpty(folderName)) { throw new ArgumentNullException("folderName"); } if (!folderName.EndsWith("\\")) { folderName += '\\'; } ulong free = 0, dummy1 = 0, dummy2 = 0; if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2)) { freespace = free; return(true); } else { return(false); } } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(false); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } return(false); }
/// <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); }
public static FileMessage Directory(String path, ref String[] files, String searchPattern, Boolean absolutePath) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { files = null; return(FileMessage.ImpersonationFailed); } else { if (System.IO.Directory.Exists(path)) { files = System.IO.Directory.GetFiles(path, searchPattern); if (absolutePath) { return(FileMessage.Read); } else { for (Int32 i = 0; i < files.Length; i++) { Int32 fileNameLength = Helper.StringReverse(files[i]).IndexOf(@"\", 1); files[i] = files[i].Substring(files[i].Length - fileNameLength); } return(FileMessage.Read); } } else { files = null; return(FileMessage.DirectoryDoesntExist); } } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static FileMessage TransmitFile(String fullFilename, HttpResponse response) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { if (System.IO.File.Exists(fullFilename)) { int chunkSize = 64; byte[] buffer = new byte[chunkSize]; int offset = 0; int read = 0; using (FileStream fs = System.IO.File.Open(fullFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) { while ((read = fs.Read(buffer, offset, chunkSize)) > 0) { response.OutputStream.Write(buffer, 0, read); response.Flush(); } } response.Close(); return(FileMessage.Read); } else { return(FileMessage.FileDoesntExist); } } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <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(); } } }
public static Byte[] File_ToByteArray(String path) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(new Byte[0]); } else { byte[] buffer; FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); try { int length = (int)fileStream.Length; // get file length, converted from long to int buffer = new byte[length]; // create buffer int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while ((count = fileStream.Read(buffer, sum, length - sum)) > 0) { sum += count; // sum is a buffer offset for next reading } } finally { fileStream.Close(); } return(buffer); } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(new Byte[0]); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <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(); } } }
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(); } } }
/// <summary> /// if the deletion of a file fails, deletion of other files continue, but the return value will be "false" /// </summary> /// <param name="Path"></param> /// <param name="Pre">Added before every path</param> /// <param name="Post">Added after every path</param> /// <returns>"false" if impersonation fails, "catch" or a single path delete fails, true in EVERY other case. </returns> public static Boolean Files(string Pre, ArrayList Path, string Post) { if (Path.Count > 0) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { Boolean retVal = true; if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(false); } else { foreach (Guid filePath in Path) { string file = Pre + filePath.ToString() + Post; retVal = Delete.File(file) && retVal; } } return(retVal); } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(false); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } } else { return(true); } }
/// <summary> /// if the deletion of a file fails, deletion of other files continue, but the return value will be "false" /// </summary> /// <param name="Path"></param> /// <returns>"false" if impersonation fails, "catch" or a single path delete fails, true in EVERY other case. </returns> public static Boolean Files(List <string> Path) { if (Path.Count > 0) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { Boolean retVal = true; if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(false); } else { foreach (string filePath in Path) { retVal = Delete.File(filePath) && retVal; } } return(retVal); } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(false); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } } else { return(true); } }
public static T Load(String path, String fileName) { T sObject = default(T); Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { throw new Exception(); } else { XmlSerializer xs = new XmlSerializer(typeof(T)); StreamReader sr = new StreamReader(path + "\\" + fileName); sObject = (T)xs.Deserialize(sr); sr.Close(); return(sObject); } } catch (Exception ex) { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } throw ex; } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static FileMessage FromStream(System.IO.Stream stream, String fileName) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { using (System.IO.Stream fileStream = System.IO.File.Create(fileName)) { for (int a = stream.ReadByte(); a != -1; a = stream.ReadByte()) { fileStream.WriteByte((byte)a); } } return(FileMessage.FileCreated); } } catch (Exception ex) { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <summary> /// Come File(), ma restituisce un enum con più informazioni /// </summary> /// <param name="path"></param> /// <returns></returns> public static FileMessage File_FM(String path) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { if (System.IO.File.Exists(path)) { return(FileMessage.FileExist); } else { return(FileMessage.FileDoesntExist); } } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
//public static Boolean isFiles(List<string> path) //{ return Files(path); } //public static Boolean isFiles(IList<string> path) //{ return Files(path); } //public static Boolean isFiles(string Pre, ArrayList Path, string Post) //{ return Files(Pre, Path, Post); } //public static Boolean isFiles(string Pre, List<Guid> Path, string Post) //{ return Files(Pre, Path, Post); } //public static Boolean isFiles(string Pre, List<string> Path, string Post) //{ return Files(Pre, Path, Post); } //public static Boolean isFile(string Path) //{ return File(Path); } #endregion public static FileMessage Directory(String path, Boolean deleteContent) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { if (System.IO.Directory.Exists(path)) { System.IO.Directory.Delete(path, deleteContent); return(FileMessage.Deleted); } else { return(FileMessage.DirectoryDoesntExist); } } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
public static T LoadOrCreate(String path, String fileName) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { throw new Exception(); } else { return(Load(path, fileName)); } } catch (Exception ex) { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); throw ex; } else { T result = default(T); SaveToFile(result, path, fileName); return(result); } } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
/// <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(); } } }
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(); } } }
public static FileMessage Directory(String path, ref List <dtoFileSystemInfo> files, String searchPattern) { Impersonate oImpersonate = new Impersonate(); Boolean wasImpersonated = Impersonate.isImpersonated(); try { if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed) { return(FileMessage.ImpersonationFailed); } else { if (System.IO.Directory.Exists(path)) { string[] fileNames = System.IO.Directory.GetFiles(path, searchPattern); if (files == null) { files = new List <dtoFileSystemInfo>(); } for (Int32 i = 0; i < fileNames.Length; i++) { try { FileInfo info = new System.IO.FileInfo(fileNames[i]); if (info != null) { files.Add(new dtoFileSystemInfo() { CreationTime = info.CreationTime, Exists = info.Exists, Extension = info.Extension, FullName = info.FullName, IsReadOnly = info.IsReadOnly, Length = info.Length, Name = info.Name }); } } catch (Exception ex) { return(FileMessage.Catch); } } return(FileMessage.Read); } else { return(FileMessage.DirectoryDoesntExist); } } } catch { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } return(FileMessage.Catch); } finally { if (!wasImpersonated) { oImpersonate.UndoImpersonation(); } } }
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(); } } }