Exemplo n.º 1
0
        public static string GetAllTextFromTextLines(VsTextMgr.IVsTextLines textLines)
        {
            string content = null;

            if (textLines != null)
            {
                int line, column;
                var result = textLines.GetLastLineIndex(out line, out column);
                if (result == VSConstants.S_OK)
                {
                    result = textLines.GetLineText(0, 0, line, column, out content);
                    if (result != VSConstants.S_OK)
                    {
                        content = null;
                    }
                }
            }
            return(content);
        }
Exemplo n.º 2
0
        public bool WriteToFile(string fullPathFileName, string content, bool saveFile, bool createIfNotExist)
        {
            var succeed = true;

            VsShell.IVsInvisibleEditor invisibleEditor = null;
            VsTextMgr.IVsTextLines     textLines       = null;
            var fileDidNotExistAndShouldBeCreated      = false;

            try
            {
                if (IsFileInRdt(fullPathFileName))
                {
                    // File is in RDT
                    textLines = GetTextLines(fullPathFileName);
                }
                else
                {
                    if (createIfNotExist &&
                        File.Exists(fullPathFileName) == false)
                    {
                        fileDidNotExistAndShouldBeCreated = true;

                        FileStream   fileStream = null;
                        StreamWriter writer     = null;
                        try
                        {
                            fileStream = File.Create(fullPathFileName);
                            // Make the newly created file unicode
                            writer = new StreamWriter(fileStream, Encoding.UTF8);

                            writer.Write(content);
                        }
                        catch (IOException e)
                        {
                            throw new InvalidOperationException(e.Message);
                        }
                        catch (ArgumentException e)
                        {
                            throw new InvalidOperationException(e.Message);
                        }
                        finally
                        {
                            if (writer != null)
                            {
                                writer.Close();
                            }
                            if (fileStream != null)
                            {
                                fileStream.Close();
                            }
                        }
                    }

                    // File is not in RDT, open it in invisible editor.
                    if (fileDidNotExistAndShouldBeCreated == false &&
                        !TryGetTextLinesAndInvisibleEditor(fullPathFileName, out invisibleEditor, out textLines))
                    {
                        // Failed to get text lines or invisible editor.
                        textLines = null;
                    }
                }

                if (fileDidNotExistAndShouldBeCreated == false)
                {
                    if (textLines != null)
                    {
                        int line, column;
                        var result = textLines.GetLastLineIndex(out line, out column);
                        if (result == VSConstants.S_OK)
                        {
                            var pContent = IntPtr.Zero;
                            try
                            {
                                // Copy the content to textLines.
                                pContent = Marshal.StringToHGlobalAuto(content);
                                result   = textLines.ReloadLines(
                                    0, 0, line, column, pContent, content.Length,
                                    new[] { new VsTextMgr.TextSpan() });
                                if (result != VSConstants.S_OK)
                                {
                                    succeed = false;
                                }
                            }
                            finally
                            {
                                if (pContent != IntPtr.Zero)
                                {
                                    Marshal.FreeHGlobal(pContent);
                                }
                            }
                            if (saveFile)
                            {
                                var list = new List <string> {
                                    fullPathFileName
                                };
                                Instance.SaveDirtyFiles(list);
                            }
                        }
                        else
                        {
                            succeed = false;
                        }
                    }
                    else
                    {
                        succeed = false;
                    }
                }
            }
            finally
            {
                // Close invisible editor from RDT
                if (invisibleEditor != null)
                {
                    Marshal.ReleaseComObject(invisibleEditor);
                }
            }
            return(succeed);
        }