/// <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; } } }