/// <summary> /// Returns a Usable Stream for that File /// </summary> /// <param name="filename">The name of the File</param> /// <param name="fa">The Acces Attributes</param> /// <param name="create">true if the file should be created if not available</param> /// <returns>a StreamItem (StreamState is Removed if the File did not exits!</returns> public static StreamItem UseStream(string filename, FileAccess fa, bool create) { StreamItem si = GetStreamItem(filename); //File does not exists, so set State to removed if (!System.IO.File.Exists(filename)) { si.Close(); si.SetFileStream(create ? new FileStream(filename, System.IO.FileMode.OpenOrCreate, fa) : null); return(si); } // Files does exist -- Removed means never opened here if (si.StreamState == StreamState.Removed) { si.SetFileStream(new FileStream(filename, FileMode.Open, fa)); } else if (!si.SetFileAccess(fa)) { si.Close(); } if (si.StreamState == StreamState.Opened) { si.FileStream.Seek(0, SeekOrigin.Begin); } return(si); }
/// <summary> /// Saves thhe MemoryStream to a File on the local Filesystem /// </summary> /// <param name="flname">The Filename</param> /// <param name="pf">The Memorystream representing the Data</param> protected void SavePackedFile(string flname, MemoryStream pf) { StreamItem si = StreamFactory.GetStreamItem(flname, false); System.IO.FileStream fs = null; if (si == null) { fs = new FileStream(flname, System.IO.FileMode.Create); } else { si.SetFileAccess(FileAccess.Write); fs = si.FileStream; } try { byte[] d = pf.ToArray(); fs.Write(d, 0, d.Length); } finally { if (si != null) { si.Close(); } else { fs.Close(); fs.Dispose(); fs = null; } } }
/// <summary> /// Closes all opened Streams (that are not locked and not referenced in the FileTable) /// </summary> /// <param name="force">true, if you want to close all Resources without checking the lock state</param> public static void CloseAll(bool force) { InitTable(); foreach (string k in streams.Keys) { if (!IsLocked(k, true) || force) { StreamItem si = streams[k] as StreamItem; si.Close(); PackageMaintainer.Maintainer.RemovePackage(k); } } }
/// <summary> /// Checks if the passed File is writable by the System /// </summary> /// <param name="flname">The FileName</param> /// <returns>true, if the File is writable</returns> public static bool CanWriteToFile(string flname, bool close) { if (!System.IO.File.Exists(flname)) { return(true); } StreamItem si = StreamFactory.UseStream(flname, System.IO.FileAccess.ReadWrite); bool res = (si.StreamState == StreamState.Opened); if (close && res) { si.Close(); } return(res); }
/// <summary> /// Closes a FileStream if opened and known by the Factory /// </summary> /// <param name="filename">The name of the File</param> /// <returns>true if the File is closed now</returns> public static bool CloseStream(string filename) { if (IsLocked(filename, false)) { return(false); } StreamItem si = GetStreamItem(filename, false); if (si != null) { si.Close(); if (!IsLocked(filename, true)) { PackageMaintainer.Maintainer.RemovePackage(filename); } return(si.StreamState != StreamState.Opened); } return(false); }