src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/CreateFileFromTemplateService.cs
/// <summary>
/// Create a file with the given template file and add it to the parent node.
/// </summary>
/// <param name="templateFile">The name of the template zip file.</param>
/// <param name="parentDocumentMoniker">The path to the node to which the new file will be added.</param>
/// <param name="fileName">The name of the file to be created.</param>
/// <returns>true if file is added successfully.</returns>
Create a file with the given template file and add it to the parent node.
Create a file with the given template file and add it to the parent node .
public async Task<bool> CreateFileAsync(string templateFile, string parentDocumentMoniker, string fileName)
        {
            Requires.NotNull(templateFile, nameof(templateFile));
            Requires.NotNullOrEmpty(parentDocumentMoniker, nameof(parentDocumentMoniker));
            Requires.NotNull(fileName, nameof(fileName));
            string templateLanguage = await GetTemplateLanguageAsync().ConfigureAwait(false);
            if (string.IsNullOrEmpty(templateLanguage))
                return false;
            await _projectVsServices.ThreadingService.SwitchToUIThread();
            string templateFilePath = _dteServices.Solution.GetProjectItemTemplate(templateFile, templateLanguage);
            if (templateFilePath != null)
            {
                HierarchyId parentId = _projectVsServices.VsProject.GetHierarchyId(parentDocumentMoniker);
                var result = new VSADDRESULT[1];
                var files = new string[] { templateFilePath };
                _projectVsServices.VsProject.AddItemWithSpecific(parentId, VSADDITEMOPERATION.VSADDITEMOP_RUNWIZARD, fileName, (uint)files.Length, files, IntPtr.Zero, 0, Guid.Empty, null, Guid.Empty, result);
                if (result[0] == VSADDRESULT.ADDRESULT_Success)
                {
                    return true;
                }
            }
            return false;
        }
public async Task < bool > CreateFileAsync ( string templateFile , string parentDocumentMoniker , string fileName ) { Requires . NotNull ( templateFile , nameof ( templateFile ) ) ; Requires . NotNullOrEmpty ( parentDocumentMoniker , nameof ( parentDocumentMoniker ) ) ; Requires . NotNull ( fileName , nameof ( fileName ) ) ; string templateLanguage = await GetTemplateLanguageAsync ( ) . ConfigureAwait ( false ) ; if ( string . IsNullOrEmpty ( templateLanguage ) ) return false ; await _projectVsServices . ThreadingService . SwitchToUIThread ( ) ; string templateFilePath = _dteServices . Solution . GetProjectItemTemplate ( templateFile , templateLanguage ) ; if ( templateFilePath != null ) { HierarchyId parentId = _projectVsServices . VsProject . GetHierarchyId ( parentDocumentMoniker ) ; var result = new VSADDRESULT [ 1 ] ; var files = new string [ ] { templateFilePath } ; _projectVsServices . VsProject . AddItemWithSpecific ( parentId , VSADDITEMOPERATION . VSADDITEMOP_RUNWIZARD , fileName , ( uint ) files . Length , files , IntPtr . Zero , 0 , Guid . Empty , null , Guid . Empty , result ) ; if ( result [ 0 ] == VSADDRESULT . ADDRESULT_Success ) { return true ; } } return false ; }
Create a file with the given template file and add it to the parent node.
Create a file with the given template file and add it to the parent node .
true if file is added successfully.
true if file is added successfully .
templateFile
string
The name of the template zip file.
The name of the template zip file .
parentDocumentMoniker
string
The path to the node to which the new file will be added.
The path to the node to which the new file will be added .
fileName
string
The name of the file to be created.
The name of the file to be created .