/// <summary> /// Writes content to file /// </summary> /// <param name="r"> </param> /// <param name="name"> </param> /// <param name="content"> </param> /// <param name="userLog"> </param> /// <param name="append"> </param> /// <returns> </returns> public static IFileNameResolver Write(this IFileNameResolver r, string name, object content, IUserLog userLog = null, bool append = false) { if (r == null) { throw new ArgumentNullException("r"); } lock (writelock) { userLog = checkUserLog(r, userLog); userLog.Debug("start write " + name); var path = r.Resolve(name, false, null, userLog); userLog.Debug("path resolved as " + path); Directory.CreateDirectory(Path.GetDirectoryName(path)); if (append) { appendFile(path, content); } else { rewriteFile(path, content); } r.ClearCache(); userLog.Trace("file saved " + path); return(r); } }
/// <summary> /// Resolves the best posible path. /// </summary> /// <param name="type"> The type. </param> /// <param name="dir"> The dir. </param> /// <param name="file"> The file. </param> /// <param name="userLog"> The UserLog. </param> /// <returns> </returns> /// <remarks> /// </remarks> private string ResolveBestPosiblePath(FileSearchResultType type, string dir, string file, IUserLog userLog) { if (file.StartsWith("~/")) { return(AdaptFilePath(type, Path.Combine(Root, ResolveStandardPath(file.Substring(2))).NormalizePath())); } var path = "/" + dir + "/" + file; var resolved = (Root + path).NormalizePath(); var result = AdaptFilePath(type, resolved); userLog.Debug("resolved to best possible " + result, Application); return(result); }
/// <summary> /// Read content of file /// </summary> /// <param name="r"> </param> /// <param name="name"> </param> /// <param name="userLog"> </param> /// <typeparam name="T"> </typeparam> /// <returns> </returns> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="ArgumentException"></exception> public static T Read <T>(this IFileNameResolver r, string name, IUserLog userLog = null) { if (r == null) { throw new ArgumentNullException("r"); } if (typeof(T) == typeof(string) || typeof(T) == typeof(XElement) || typeof(T) == typeof(byte[])) { userLog = checkUserLog(r, userLog); userLog.Debug("start write " + name); var path = r.Resolve(name, true, null, userLog); if (null == path) { userLog.Error("file not found"); throw new FileNotFoundException(name); } userLog.Debug("path resolved as " + path); object result = default(T); if (typeof(T) == typeof(string)) { result = File.ReadAllText(path); } else if (typeof(T) == typeof(byte)) { result = File.ReadAllBytes(path); } else if (typeof(T) == typeof(XElement)) { result = XElement.Load(path); } userLog.Trace("file readed " + path); return((T)result); } throw new ArgumentException("Read method supports only strings, XElement and byte[] as result"); }
/// <summary> /// Resolves the best posible path. /// </summary> /// <param name="type"> The type. </param> /// <param name="dir"> The dir. </param> /// <param name="file"> The file. </param> /// <param name="userLog"> The UserLog. </param> /// <returns> </returns> /// <remarks> /// </remarks> private string ResolveBestPosiblePath(FileSearchResultType type, string dir, string file, IUserLog userLog) { if (file.StartsWith("~/")) { return AdaptFilePath(type,Path.Combine(Root, ResolveStandardPath( file.Substring(2))).NormalizePath()); } var path = "/" + dir + "/" + file; var resolved = (Root + path).NormalizePath(); var result = AdaptFilePath(type, resolved); userLog.Debug("resolved to best possible " + result, Application); return result; }