GetStream() public méthode

public GetStream ( ) : Stream
Résultat Stream
        public static void OpenFileInShellWith(PackageFile file)
        {
            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            string tempFileName = Path.Combine(GetTempFilePath(), file.Name);

            using (Stream tempFileStream = File.Create(tempFileName))
            {
                file.GetStream().CopyTo(tempFileStream);
            }

            if (File.Exists(tempFileName))
            {
                var info = new ProcessStartInfo("rundll32.exe")
                           {
                               ErrorDialog = true,
                               UseShellExecute = false,
                               Arguments =
                                   "shell32.dll,OpenAs_RunDLL " + tempFileName
                           };

                Process.Start(info);
            }
        }
        public static void OpenFileInShell(PackageFile file, IUIServices uiServices)
        {
            if (IsExecutableScript(file.Name))
            {
                bool confirm = uiServices.Confirm(
                    String.Format(CultureInfo.CurrentCulture, Resources.OpenExecutableScriptWarning_Title, file.Name),
                    Resources.OpenExecutableScriptWarning,
                    isWarning: true);
                if (!confirm)
                {
                    return;
                }
            }

            // copy to temporary file
            // create package in the temprary file first in case the operation fails which would
            // override existing file with a 0-byte file.
            string tempFileName = Path.Combine(GetTempFilePath(), file.Name);
            using (Stream tempFileStream = File.Create(tempFileName))
            {
                file.GetStream().CopyTo(tempFileStream);
            }

            if (File.Exists(tempFileName))
            {
                Process.Start("explorer.exe", tempFileName);
            }
        }
        private static string ReadFileContent(PackageFile file, out long size) {
            const int MaxLengthToOpen = 10 * 1024;      // limit to 10K 
            const int BufferSize = 2 * 1024;
            char[] buffer = new char[BufferSize];       // read 2K at a time

            StringBuilder sb = new StringBuilder();
            Stream stream = file.GetStream();
            size = stream.Length;
            using (StreamReader reader = new StreamReader(stream)) {
                while (sb.Length < MaxLengthToOpen) {
                    int bytesRead = reader.Read(buffer, 0, BufferSize);
                    if (bytesRead == 0) {
                        break;
                    }
                    else {
                        sb.Append(new string(buffer, 0, bytesRead));
                    }
                }

                // if not reaching the end of the stream yet, append the text "Truncating..."
                if (reader.Peek() > -1) {
                    // continue reading the rest of the current line to avoid dangling line
                    sb.AppendLine(reader.ReadLine());

                    if (reader.Peek() > -1) {
                        sb.AppendLine().AppendLine("*** The rest of the content is truncated. ***");
                    }
                }
            }

            return sb.ToString();
        }
        private void ShowFile(PackageFile file) {
            bool isBinary = IsBinaryFile(file.Name);
            long size;
            string content;

            if (isBinary) {
                content = Resources.UnsupportedFormatMessage;
                using (Stream stream = file.GetStream()) {
                    size = stream.Length;
                }
            }
            else {
                content = ReadFileContent(file, out size);
            }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size,
                DetermineLanguage(file.Name));

            ViewModel.ShowFile(fileInfo);
        }
        public void AddFile(PackageFile file, bool makeCopy = false)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (Contains(file))
            {
                return;
            }

            PackagePart newFile;

            if (makeCopy)
            {
                string fileCopyPath;
                using (Stream originalFileStream = file.GetStream())
                {
                    fileCopyPath = FileHelper.CreateTempFile(file.Name, originalFileStream);
                }

                string newTargetPath = this.Path + "\\" + file.Name;
                var physicalFile = new PhysicalPackageFile(isTempFile: true, originalPath: fileCopyPath, targetPath: newTargetPath);

                newFile = new PackageFile(physicalFile, file.Name, this);
            }
            else
            {
                // detach from current parent
                if (file.Parent != null)
                {
                    file.Parent.Detach(file);
                }

                newFile = file;
            }

            Attach(newFile);
            newFile.IsSelected = true;
            IsExpanded = true;
            PackageViewModel.NotifyChanges();
        }
Exemple #6
0
 private void SaveContentExecute(PackageFile file)
 {
     string selectedFileName;
     string title = "Save " + file.Name;
     string filter = "All files (*.*)|*.*";
     if (UIServices.OpenSaveFileDialog(title, file.Name, filter, out selectedFileName)) {
         using (FileStream fileStream = File.OpenWrite(selectedFileName)) {
             file.GetStream().CopyTo(fileStream);
         }
     }
 }
        private void ShowFile(PackageFile file)
        {
            long size = -1;
            object content = null;
            bool isBinary = false;

            // find all plugins which can handle this file's extension
            IEnumerable<IPackageContentViewer> contentViewers = FindContentViewer(file);
            if (contentViewers != null)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (IPackageContentViewer viewer in contentViewers)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            if (size == -1)
                            {
                                size = stream.Length;
                            }
                            content = viewer.GetView(Path.GetExtension(file.Name), stream);
                            if (content != null)
                            {
                                // found a plugin that can read this file, stop
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    // don't calculate the size again if we already have it
                    if (size == -1)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            size = stream.Length;
                        }
                    }
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out size);
                }
            }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size);

            ViewModel.ShowFile(fileInfo);
        }
Exemple #8
0
        private void ShowFile(PackageFile file)
        {
            object content  = null;
            var    isBinary = false;

            // find all plugins which can handle this file's extension
            var contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (var viewer in contentViewers)
                    {
                        using (var stream = file.GetStream())
                        {
                            content = viewer.GetView(Path.GetExtension(file.Name), stream);
                            if (content != null)
                            {
                                // found a plugin that can read this file, stop
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            long size = -1;

            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out size);
                }
            }

            if (size == -1)
            {
                // This is inefficient but cn be cleaned up later
                using (var str = file.GetStream())
                    using (var ms = new MemoryStream())
                    {
                        str.CopyTo(ms);
                        size = ms.Length;
                    }
            }


            IReadOnlyList <AuthenticodeSignature> sigs;
            SignatureCheckResult isValidSig;

            using (var str = file.GetStream())
                using (var tempFile = new TemporaryFile(str, Path.GetExtension(file.Name)))
                {
                    var extractor = new FileInspector(tempFile.FileName);

                    sigs       = extractor.GetSignatures().ToList();
                    isValidSig = extractor.Validate();
                }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size,
                sigs,
                isValidSig);

            ViewModel.ShowFile(fileInfo);
        }
        private void ShowFile(PackageFile file)
        {
            object?content  = null;
            var    isBinary = false;

            // find all plugins which can handle this file's extension
            var contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (var viewer in contentViewers)
                    {
                        var files = file.GetAssociatedFiles().ToList();

                        content = viewer.GetView(file, files);
                        if (content != null)
                        {
                            // found a plugin that can read this file, stop
                            break;
                        }
                    }
                }
                catch (Exception ex) when(!(ex is FileNotFoundException))
                {
                    DiagnosticsClient.TrackException(ex, ViewModel.Package, ViewModel.PublishedOnNuGetOrg);
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            var truncated = false;

            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out truncated);
                }
            }

            long size = -1;
            IReadOnlyList <AuthenticodeSignature> sigs;
            SignatureCheckResult isValidSig;

            using (var str = file.GetStream())
                using (var tempFile = new TemporaryFile(str, Path.GetExtension(file.Name)))
                {
                    var extractor = new FileInspector(tempFile.FileName);

                    sigs       = extractor.GetSignatures().ToList();
                    isValidSig = extractor.Validate();

                    size = tempFile.Length;
                }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size,
                truncated,
                sigs,
                isValidSig);

            ViewModel.ShowFile(fileInfo);
        }
        private void ShowFile(PackageFile file)
        {
            long   size     = -1;
            object content  = null;
            bool   isBinary = false;

            // find all plugins which can handle this file's extension
            IEnumerable <IPackageContentViewer> contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (IPackageContentViewer viewer in contentViewers)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            if (size == -1)
                            {
                                size = stream.Length;
                            }
                            content = viewer.GetView(Path.GetExtension(file.Name), stream);
                            if (content != null)
                            {
                                // found a plugin that can read this file, stop
                                break;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent;
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    // don't calculate the size again if we already have it
                    if (size == -1)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            size = stream.Length;
                        }
                    }
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out size);
                }
            }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size);

            ViewModel.ShowFile(fileInfo);
        }
 private void SaveContentExecute(PackageFile file)
 {
     string selectedFileName;
     string title = "Save " + file.Name;
     const string filter = "All files (*.*)|*.*";
     int filterIndex;
     if (UIServices.OpenSaveFileDialog(title, file.Name, /* initial directory */ null, filter, /* overwritePrompt */ true,
                                       out selectedFileName, out filterIndex))
     {
         using (FileStream fileStream = File.OpenWrite(selectedFileName))
         {
             file.GetStream().CopyTo(fileStream);
         }
     }
 }
Exemple #12
0
        private void ShowFile(PackageFile file)
        {
            object content  = null;
            bool   isBinary = false;

            // find all plugins which can handle this file's extension
            IEnumerable <IPackageContentViewer> contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                isBinary = true;
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (IPackageContentViewer viewer in contentViewers)
                    {
                        using (Stream stream = file.GetStream())
                        {
                            content = viewer.GetView(Path.GetExtension(file.Name), stream);
                            if (content != null)
                            {
                                // found a plugin that can read this file, stop
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                if (content is string)
                {
                    isBinary = false;
                }
            }

            // if plugins fail to read this file, fall back to the default viewer
            long size = -1;

            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out size);
                }
            }

            if (size == -1)
            {
                // This is inefficient but cn be cleaned up later
                using (var str = file.GetStream())
                    using (var ms = new MemoryStream())
                    {
                        str.CopyTo(ms);
                        size = ms.Length;
                    }
            }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size);

            ViewModel.ShowFile(fileInfo);
        }
Exemple #13
0
        private void ShowFile(PackageFile file)
        {
            object?content  = null;
            var    isBinary = false;

            // find all plugins which can handle this file's extension
            var contentViewers = FindContentViewer(file);

            if (contentViewers != null)
            {
                try
                {
                    // iterate over all plugins, looking for the first one that return non-null content
                    foreach (var viewer in contentViewers)
                    {
                        var files = file.GetAssociatedPackageFiles().ToList();

                        content = viewer.GetView(file, files);
                        if (content != null)
                        {
                            // found a plugin that can read this file, stop
                            break;
                        }
                    }
                }
                catch (Exception ex) when(!(ex is FileNotFoundException))
                {
                    DiagnosticsClient.TrackException(ex, ViewModel.Package, ViewModel.PublishedOnNuGetOrg);
                    // don't let plugin crash the app
                    content = Resources.PluginFailToReadContent + Environment.NewLine + ex.ToString();
                }

                isBinary = content is not string;
            }

            // if plugins fail to read this file, fall back to the default viewer
            var truncated = false;

            if (content == null)
            {
                isBinary = FileHelper.IsBinaryFile(file.Name);
                if (isBinary)
                {
                    content = Resources.UnsupportedFormatMessage;
                }
                else
                {
                    content = ReadFileContent(file, out truncated);
                }
            }

            long size = -1;
            IReadOnlyList <AuthenticodeSignature> sigs;
            SignatureCheckResult isValidSig;
            {
                // note: later, throught binding converter, SigningCertificate's CN value is extracted through native api
                if (AppCompat.IsSupported(RuntimeFeature.Cryptography, RuntimeFeature.NativeMethods))
                {
                    using var stream   = file.GetStream();
                    using var tempFile = new TemporaryFile(stream, Path.GetExtension(file.Name));
                    var extractor = new FileInspector(tempFile.FileName);

                    sigs       = extractor.GetSignatures().ToList();
                    isValidSig = extractor.Validate();
                    size       = tempFile.Length;
                }
                else
                {
                    using var stream = StreamUtility.MakeSeekable(file.GetStream(), disposeOriginal: true);
                    var peFile      = new PeFile(stream);
                    var certificate = CryptoUtility.GetSigningCertificate(peFile);

                    if (certificate is not null)
                    {
                        sigs       = new List <AuthenticodeSignature>(0);
                        isValidSig = SignatureCheckResult.UnknownProvider;
                    }
                    else
                    {
                        sigs       = new List <AuthenticodeSignature>(0);
                        isValidSig = SignatureCheckResult.NoSignature;
                    }
                    size = peFile.FileSize;
                }
            }

            var fileInfo = new FileContentInfo(
                file,
                file.Path,
                content,
                !isBinary,
                size,
                truncated,
                sigs,
                isValidSig);

            ViewModel.ShowFile(fileInfo);
        }