src/Misc/EFTools/DesignXmlCore/Common/RdtManager.cs
/// <summary>
///     Refresh a file content with passed in string.  Save or not save the file according to
///     passed in option.
/// </summary>
/// <remarks>
///     This method created for refactoring usage, refactoring will work on all kinds of
///     docdata, not only SqlEditorDocData.  If change this method, please get let LiangZ
///     know.
/// </remarks>
/// <param name="fullPathFileName">File name with full path.</param>
/// <param name="content">string content need to update the file with.</param>
/// <param name="saveFile">Save file or not after writing.</param>
/// <param name="createIfNotExist">Creates the file if it doesn't exist.</param>
Refresh a file content with passed in string.  Save or not save the file according to
passed in option.
Refresh a file content with passed in string . Save or not save the file according to passed in option .
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        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))
                {
                    textLines = GetTextLines(fullPathFileName);
                }
                else
                {
                    if (createIfNotExist &&
                        File.Exists(fullPathFileName) == false)
                    {
                        fileDidNotExistAndShouldBeCreated = true;
                        FileStream fileStream = null;
                        StreamWriter writer = null;
                        try
                        {
                            fileStream = File.Create(fullPathFileName);
                            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();
                            }
                        }
                    }
                    if (fileDidNotExistAndShouldBeCreated == false
                        && !TryGetTextLinesAndInvisibleEditor(fullPathFileName, out invisibleEditor, out textLines))
                    {
                        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
                            {
                                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
            {
                if (invisibleEditor != null)
                {
                    Marshal.ReleaseComObject(invisibleEditor);
                }
            }
            return succeed;
        }
[ SuppressMessage ( " " , " " ) ] 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 ) ) { textLines = GetTextLines ( fullPathFileName ) ; } else { if ( createIfNotExist && File . Exists ( fullPathFileName ) == false ) { fileDidNotExistAndShouldBeCreated = true ; FileStream fileStream = null ; StreamWriter writer = null ; try { fileStream = File . Create ( fullPathFileName ) ; 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 ( ) ; } } } if ( fileDidNotExistAndShouldBeCreated == false && ! TryGetTextLinesAndInvisibleEditor ( fullPathFileName , out invisibleEditor , out textLines ) ) { 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 { 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 { if ( invisibleEditor != null ) { Marshal . ReleaseComObject ( invisibleEditor ) ; } } return succeed ; }
Refresh a file content with passed in string.
Refresh a file content with passed in string .
// File is in RDT // Make the newly created file unicode // File is not in RDT, open it in invisible editor. // Failed to get text lines or invisible editor. // Copy the content to textLines. // Close invisible editor from RDT
fullPathFileName
string
File name with full path.
File name with full path .
content
string
string content need to update the file with.
string content need to update the file with .
saveFile
bool
Save file or not after writing.
Save file or not after writing .
createIfNotExist
bool
Creates the file if it doesn't exist.
Creates the file if it doesn ' t exist .
remarks
This method created for refactoring usage, refactoring will work on all kinds of
docdata, not only SqlEditorDocData.  If change this method, please get let LiangZ
know.
This method created for refactoring usage refactoring will work on all kinds of docdata not only SqlEditorDocData . If change this method please get let LiangZ know .