/// <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); }
public static void WriteToConsole() { InitTable(); System.Windows.Forms.Form f = new System.Windows.Forms.Form(); System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox(); f.Controls.Add(lb); lb.Dock = System.Windows.Forms.DockStyle.Fill; foreach (string k in streams.Keys) { StreamItem si = streams[k] as StreamItem; string add = k; if (si != null) { add += " [" + si.StreamState + "]"; } if (IsLocked(k, false)) { add = "[locked] " + add; } else if (IsLocked(k, true)) { add = "[ftlocked] " + add; } if (PackageMaintainer.Maintainer.Contains(k)) { add += "[managed]"; } lb.Items.Add(add); } lb.Sorted = true; f.ShowDialog(); f.Dispose(); }
/// <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> /// Returns a valid stream Item for the passed Filename /// </summary> /// <param name="filename">The name of the FIle you want to open</param> /// <returns>a valid StreamItem or null if not found and createnew was set</returns> /// <param name="createnew"> /// If true and this File was not know yet, a new StreamItem will be generated /// for it and returned. The StreamItem will /// not contain a Stream in that case! /// </param> public static StreamItem GetStreamItem(string filename, bool createnew) { InitTable(); if (filename == null) { filename = ""; } filename = filename.Trim().ToLower(); StreamItem si = (StreamItem)streams[filename]; if ((si == null) && createnew) { si = new StreamItem(null); streams[filename] = si; } return(si); }
/// <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); }
/// <summary> /// Returns true if a FileStream for this file exists /// </summary> /// <param name="name"></param> /// <returns></returns> public static bool IsStreamAvailable(string name) { StreamItem si = GetStreamItem(name, false); return(si != null); }