System.Configuration.Internal.InternalConfigHost.StaticOpenStreamForWrite C# (CSharp) Метод

StaticOpenStreamForWrite() статический приватный Метод

static private StaticOpenStreamForWrite ( string streamName, string templateStreamName, object &writeContext, bool assertPermissions ) : Stream
streamName string
templateStreamName string
writeContext object
assertPermissions bool
Результат System.IO.Stream
        static internal Stream StaticOpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext, bool assertPermissions) {
            bool revertAssert = false;
            
            if (string.IsNullOrEmpty(streamName)) {
                throw new ConfigurationException(SR.GetString(SR.Config_no_stream_to_write));
            }

            // Create directory if it does not exist.
            // Ignore errors, allow any failure to come when trying to open the file.
            string dir = Path.GetDirectoryName(streamName);
            try {
                if (!Directory.Exists(dir)) {
                    if (assertPermissions) {
                        new FileIOPermission(PermissionState.Unrestricted).Assert();
                        revertAssert = true;
                    }
                    
                    Directory.CreateDirectory(dir);
                }
            }
            catch {
            }
            finally {
                if (revertAssert) {
                    CodeAccessPermission.RevertAssert();
                }
            }

            Stream stream;
            WriteFileContext writeFileContext = null;
            revertAssert = false;

            if (assertPermissions) {
                // If we're asked to assert permission, we will assert allAccess on the directory (instead of just the file).
                // We need to assert for the whole directory because WriteFileContext will call TempFileCollection.AddExtension,
                // which will generate a temporary file and make a AllAccess Demand on that file.
                // Since we don't know the name of the temporary file right now, we need to assert for the whole dir.
                new FileIOPermission(FileIOPermissionAccess.AllAccess, dir).Assert();
                revertAssert = true;
            }

            try {
                writeFileContext = new WriteFileContext(streamName, templateStreamName);

                if (File.Exists(streamName)) {
                    FileInfo fi = new FileInfo(streamName);
                    FileAttributes attrs = fi.Attributes;
                    if ((int)(attrs & InvalidAttributesForWrite) != 0) {
                        throw new IOException(SR.GetString(SR.Config_invalid_attributes_for_write, streamName));
                    }
                }

                try {
                    stream = new FileStream(writeFileContext.TempNewFilename, FileMode.Create, FileAccess.Write, FileShare.Read);
                }
                // Wrap all exceptions so that we provide a meaningful filename - otherwise the end user
                // will just see the temporary file name, which is meaningless.
                catch (Exception e) {
                    throw new ConfigurationException(SR.GetString(SR.Config_write_failed, streamName), e);
                }
                catch {
                    throw new ConfigurationException(SR.GetString(SR.Config_write_failed, streamName));
                }
            }
            catch {
                if (writeFileContext != null) {
                    writeFileContext.Complete(streamName, false);
                }
                throw;
            }
            finally {
                if (revertAssert) {
                    CodeAccessPermission.RevertAssert();
                }
            }

            writeContext = writeFileContext;
            return stream;
        }