コード例 #1
0
        private void CreateStreams(string filePath, string streamName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, Encoding fileEncoding)
        {
            if (File.Exists(filePath) && (this.provider.Force != 0))
            {
                this.oldAttributes     = File.GetAttributes(filePath);
                this.haveOldAttributes = true;
                System.IO.FileAttributes hidden = System.IO.FileAttributes.Hidden;
                if ((fileAccess & FileAccess.Write) != 0)
                {
                    hidden |= System.IO.FileAttributes.ReadOnly;
                }
                File.SetAttributes(this.path, File.GetAttributes(filePath) & ~hidden);
            }
            FileAccess access = fileAccess;

            if ((fileAccess & FileAccess.Write) != 0)
            {
                fileAccess = FileAccess.ReadWrite;
            }
            try
            {
                if (!string.IsNullOrEmpty(streamName))
                {
                    this.stream = AlternateDataStreamUtilities.CreateFileStream(filePath, streamName, fileMode, fileAccess, fileShare);
                }
                else
                {
                    this.stream = new FileStream(filePath, fileMode, fileAccess, fileShare);
                }
            }
            catch (IOException)
            {
                if (!string.IsNullOrEmpty(streamName))
                {
                    this.stream = AlternateDataStreamUtilities.CreateFileStream(filePath, streamName, fileMode, access, fileShare);
                }
                else
                {
                    this.stream = new FileStream(filePath, fileMode, access, fileShare);
                }
            }
            if (!this.usingByteEncoding)
            {
                if ((fileAccess & FileAccess.Read) != 0)
                {
                    this.reader      = new StreamReader(this.stream, fileEncoding);
                    this._backReader = new FileStreamBackReader(this.stream, fileEncoding);
                }
                if ((fileAccess & FileAccess.Write) != 0)
                {
                    if ((this.reader != null) && ((fileAccess & FileAccess.Read) != 0))
                    {
                        this.reader.Peek();
                        fileEncoding = this.reader.CurrentEncoding;
                    }
                    this.writer = new StreamWriter(this.stream, fileEncoding);
                }
            }
        }
コード例 #2
0
        } // Read

        private void CreateStreams(string filePath, string streamName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, Encoding fileEncoding)
        {
            // Try to mask off the ReadOnly, and Hidden attributes
            // if they've specified Force.
            if (File.Exists(filePath) && _provider.Force)
            {
                // Store the old attributes so that we can recover them
                // in the Close();
                _oldAttributes = File.GetAttributes(filePath);
                _haveOldAttributes = true;

                // Clear the hidden attribute, and if we're writing, also clear readonly.
                var attributesToClear = FileAttributes.Hidden;
                if ((fileAccess & (FileAccess.Write)) != 0)
                {
                    attributesToClear |= FileAttributes.ReadOnly;
                }
                File.SetAttributes(_path, (File.GetAttributes(filePath) & ~attributesToClear));
            }

            // If we want to write to the stream, attempt to open it for reading as well
            // so that we can determine the file encoding as we append to it
            FileAccess requestedAccess = fileAccess;
            if ((fileAccess & (FileAccess.Write)) != 0)
            {
                fileAccess = FileAccess.ReadWrite;
            }

            try
            {
                if (!String.IsNullOrEmpty(streamName))
                {
                    _stream = AlternateDataStreamUtilities.CreateFileStream(filePath, streamName, fileMode, fileAccess, fileShare);
                }
                else
                {
                    _stream = new FileStream(filePath, fileMode, fileAccess, fileShare);
                }
            }
            catch (IOException)
            {
                if (!String.IsNullOrEmpty(streamName))
                {
                    _stream = AlternateDataStreamUtilities.CreateFileStream(filePath, streamName, fileMode, requestedAccess, fileShare);
                }
                else
                {
                    _stream = new FileStream(filePath, fileMode, requestedAccess, fileShare);
                }
            }

            if (!_usingByteEncoding)
            {
                // Open the reader stream
                if ((fileAccess & (FileAccess.Read)) != 0)
                {
                    _reader = new StreamReader(_stream, fileEncoding);
                    _backReader = new FileStreamBackReader(_stream, fileEncoding);
                }

                // Open the writer stream
                if ((fileAccess & (FileAccess.Write)) != 0)
                {
                    // Ensure we are using the proper encoding
                    if ((_reader != null) &&
                        ((fileAccess & (FileAccess.Read)) != 0))
                    {
                        _reader.Peek();
                        fileEncoding = _reader.CurrentEncoding;
                    }

                    _writer = new StreamWriter(_stream, fileEncoding);
                }
            }
        }
コード例 #3
0
 private void CreateStreams(string filePath, string streamName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, Encoding fileEncoding)
 {
     if (File.Exists(filePath) && (this.provider.Force != 0))
     {
         this.oldAttributes = File.GetAttributes(filePath);
         this.haveOldAttributes = true;
         System.IO.FileAttributes hidden = System.IO.FileAttributes.Hidden;
         if ((fileAccess & FileAccess.Write) != 0)
         {
             hidden |= System.IO.FileAttributes.ReadOnly;
         }
         File.SetAttributes(this.path, File.GetAttributes(filePath) & ~hidden);
     }
     FileAccess access = fileAccess;
     if ((fileAccess & FileAccess.Write) != 0)
     {
         fileAccess = FileAccess.ReadWrite;
     }
     try
     {
         if (!string.IsNullOrEmpty(streamName))
         {
             this.stream = AlternateDataStreamUtilities.CreateFileStream(filePath, streamName, fileMode, fileAccess, fileShare);
         }
         else
         {
             this.stream = new FileStream(filePath, fileMode, fileAccess, fileShare);
         }
     }
     catch (IOException)
     {
         if (!string.IsNullOrEmpty(streamName))
         {
             this.stream = AlternateDataStreamUtilities.CreateFileStream(filePath, streamName, fileMode, access, fileShare);
         }
         else
         {
             this.stream = new FileStream(filePath, fileMode, access, fileShare);
         }
     }
     if (!this.usingByteEncoding)
     {
         if ((fileAccess & FileAccess.Read) != 0)
         {
             this.reader = new StreamReader(this.stream, fileEncoding);
             this._backReader = new FileStreamBackReader(this.stream, fileEncoding);
         }
         if ((fileAccess & FileAccess.Write) != 0)
         {
             if ((this.reader != null) && ((fileAccess & FileAccess.Read) != 0))
             {
                 this.reader.Peek();
                 fileEncoding = this.reader.CurrentEncoding;
             }
             this.writer = new StreamWriter(this.stream, fileEncoding);
         }
     }
 }