本文整理汇总了C#中ProjectItems.AddFolder方法的典型用法代码示例。如果您正苦于以下问题:C# ProjectItems.AddFolder方法的具体用法?C# ProjectItems.AddFolder怎么用?C# ProjectItems.AddFolder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ProjectItems
的用法示例。
在下文中一共展示了ProjectItems.AddFolder方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyProjectItems
private static void CopyProjectItems(ProjectItems sourceItems, ProjectItems targetItems)
{
foreach (ProjectItem projectItem in sourceItems)
{
ProjectItem tempItem = null;
try
{
tempItem = targetItems.Item(projectItem.Name);
}
catch (ArgumentException)
{
if (projectItem.Kind == "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}")
{
if (projectItem.Name != "Properties")
{
tempItem = targetItems.AddFolder(projectItem.Name);
}
}
else if (projectItem.Name != "packages.config")
{
tempItem = targetItems.AddFromFile(projectItem.Properties.Item("LocalPath").Value as string);
}
}
if (tempItem != null)
{
CopyProjectItems(projectItem.ProjectItems, tempItem.ProjectItems);
}
}
}
示例2: CreateFolder
private static ProjectItem CreateFolder(ProjectItems currentItems, string container)
{
var folderName = container;
int index = 1;
// Keep looking for a unique name as long as we collide with some item.
// NOTE(cyrusn): There shouldn't be a race condition here. While it looks like a rogue
// component could stomp on the name we've found once we've decided on it, they really
// can't since we're running on the main thread. And, if someone does stomp on us
// somehow, then we really should just throw in that case.
while (currentItems.FindItem(folderName, StringComparer.OrdinalIgnoreCase) != null)
{
folderName = container + index;
index++;
}
return currentItems.AddFolder(folderName);
}
示例3: AddFilesAndFolders
/// <summary>
/// Adds the files and folders.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="targetProjectType">Type of the target project.</param>
/// <param name="levels">The number of levels to walk down the chain. If <c>-1</c>, it will handle all levels.</param>
/// <param name="fileFilter">An enumerable of files that should be handled with care, can be <c>null</c>.</param>
/// <exception cref="ArgumentNullException">The <paramref name="source" /> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">The <paramref name="target" /> is <c>null</c>.</exception>
private void AddFilesAndFolders(ProjectItems source, ProjectItems target, ProjectType targetProjectType, int levels, IEnumerable<string> fileFilter)
{
Argument.IsNotNull("source", source);
Argument.IsNotNull("target", target);
string targetParentName = target.Parent.GetObjectName();
Log.Debug("Adding files and folders to target '{0}'", targetParentName);
if (levels == 0)
{
return;
}
levels--;
foreach (ProjectItem sourceItem in source)
{
var sourceItemName = sourceItem.Name;
if (sourceItem.IsLinkedFile())
{
Log.Debug("Skipping item '{0}' because it is a linked file (so has another root project)", sourceItem.GetObjectName());
continue;
}
if (ShouldSkipAddingOfItem(sourceItem, targetProjectType))
{
Log.Debug("Skipping item '{0}' because it is ignored by a rule for target project {1}", sourceItem.GetObjectName(), targetProjectType);
continue;
}
ProjectItem existingTargetItem = null;
foreach (ProjectItem targetItem in target)
{
if (string.Equals(targetItem.Name, sourceItemName))
{
existingTargetItem = targetItem;
break;
}
}
bool isFolder = sourceItem.IsFolder();
bool containsSubItems = isFolder || sourceItem.ContainsChildItems();
if (existingTargetItem == null)
{
if (!isFolder)
{
if (sourceItem.IsXamlFile() && ((targetProjectType == ProjectType.NET40) || (targetProjectType == ProjectType.NET45)))
{
Log.Debug("File '{0}' is a xaml file and the target project is NET40 or NET45. There is a bug in Visual Studio that does not allow to link xaml files in NET40, so a copy is created.", sourceItem.FileNames[0]);
// string targetFile = sourceItem.GetTargetFileName(target);
// File.Copy(sourceItem.FileNames[0], targetFile);
existingTargetItem = target.AddFromFileCopy(sourceItem.FileNames[0]);
}
else
{
Log.Debug("Adding link to file '{0}'", sourceItem.FileNames[0]);
try
{
// Linked file
existingTargetItem = target.AddFromFile(sourceItem.FileNames[0]);
}
catch (Exception ex)
{
var messageService = ServiceLocator.Default.ResolveType<IMessageService>();
messageService.ShowError(ex);
}
}
}
else
{
Log.Debug("Adding folder '{0}'", sourceItem.FileNames[0]);
string targetDirectory = sourceItem.GetTargetFileName(target.ContainingProject);
existingTargetItem = Directory.Exists(targetDirectory) ? target.AddFromDirectory(targetDirectory) : target.AddFolder(sourceItem.Name);
}
if (existingTargetItem != null)
{
Log.Debug("Added item '{0}'", existingTargetItem.Name);
}
}
bool isResourceFile = sourceItem.IsResourceFile();
//.........这里部分代码省略.........
示例4: CopyFolder
private void CopyFolder(DTE dte, string sourceFolder, string destFolder, ProjectItems parentProjectItems)
{
//first copy all files
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = Path.GetFileName(file);
string dest = Path.Combine(destFolder, name);
if (File.Exists(dest))
{
//if (MessageBox.Show("Project file " + name + " already exists. Overwrite?", "Overwrite file", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//Helpers.WriteToOutputWindow(dte, "Warning: File " + dest + " replaced.", false);
File.Copy(file, dest, true);
}
}
else
{
File.Copy(file, dest, true);
}
try
{ //add file to project items
ProjectItem addItem = AddFromFile(parentProjectItems, dest);
addItem.Properties.Item("BuildAction").Value = 2;
}
catch (Exception)
{ //file already existed
}
}
//second create the directories in the project if they are not there
string[] sourcefolders = Directory.GetDirectories(sourceFolder);
foreach (string sourcefolder in sourcefolders)
{
string name = Path.GetFileName(sourcefolder);
string dest = Path.Combine(destFolder, name);
//create folder in the project if it not exists
ProjectItem projectFolder = null;
try
{
projectFolder = GetProjectItemByName(parentProjectItems, name);
}
catch (Exception)
{
}
if (projectFolder == null)
{
projectFolder = parentProjectItems.AddFolder(name, EnvDTE.Constants.vsProjectItemKindPhysicalFolder);
}
CopyFolder(dte, sourcefolder, dest, projectFolder.ProjectItems);
}
}
示例5: AddOrCreateFolder
private ProjectItem AddOrCreateFolder(string folderName, ProjectItems parentCollection)
{
try
{
ProjectItem addedFolderProjectItem = parentCollection.AddFolder(folderName, Constants.vsProjectItemKindPhysicalFolder);
string relativePath = GetPathRelativeToProject(TargetProject, addedFolderProjectItem.Properties.GetValue("FullPath", string.Empty));
logger.Log(String.Format(CultureInfo.CurrentCulture, Resources.FolderCreatedInTarget, relativePath, TargetProject.Name));
return addedFolderProjectItem;
}
catch (COMException comEx)
{
if (comEx.ErrorCode == -2147467259) //HRESULT 0x80004005
{
return IncludeExistingFolder(folderName, parentCollection);
}
throw;
}
}
示例6: AddFolder
/// <summary>
/// Adds a folder to a specified <paramref name="collection" /> of project items.
/// </summary>
/// <param name="collection">
/// A <see cref="ProjectItems" /> collection that belongs to a <see cref="Project" /> or
/// <see cref="ProjectItem" /> of type <see cref="EnvDTE.Constants.vsProjectItemKindPhysicalFolder" />.
/// </param>
/// <param name="folderName">
/// Name of the folder to be added.
/// </param>
/// <param name="basePath">
/// Absolute path to the directory where the folder is located.
/// </param>
/// <returns>
/// A <see cref="ProjectItem" /> that represents new folder added to the <see cref="Project" />.
/// </returns>
/// <remarks>
/// If the specified folder doesn't exist in the solution and the file system,
/// a new folder will be created in both. However, if the specified folder
/// already exists in the file system, it will be added to the solution instead.
/// Unfortunately, an existing folder can only be added to the solution with
/// all of sub-folders and files in it. Thus, if a single output file is
/// generated in an existing folders not in the solution, the target folder will
/// be added to the solution with all files in it, generated or not. The
/// only way to avoid this would be to immediately remove all child items
/// from a newly added existing folder. However, this could lead to having
/// orphaned files that were added to source control and later excluded from
/// the project. We may need to revisit this code and access <see cref="SourceControl" />
/// automation model to remove the child items from source control too.
/// </remarks>
private static ProjectItem AddFolder(ProjectItems collection, string folderName, string basePath)
{
// Does the folder already exist in the solution?
ProjectItem folder = collection.Cast<ProjectItem>().FirstOrDefault(p => string.Equals(p.Name, folderName, StringComparison.OrdinalIgnoreCase));
if (folder != null)
{
return folder;
}
try
{
// Try adding folder to the project.
// Note that this will work for existing folder in a Database project but not in C#.
return collection.AddFolder(folderName);
}
catch (COMException)
{
// If folder already exists on disk and the previous attempt to add failed
string folderPath = Path.Combine(basePath, folderName);
if (Directory.Exists(folderPath))
{
// Try adding it from disk
// Note that this will work in a C# but is not implemented in Database projects.
return collection.AddFromDirectory(folderPath);
}
throw;
}
}
示例7: AddFromFileWithFolders
private void AddFromFileWithFolders(ProjectItems root, string name, string dir)
{
string pathToNow = null;
var tmp = name.Split('\\');
for (int i = 0; i < tmp.Length - 1; i++)
{
string thisDir = tmp[i];
pathToNow = pathToNow == null ? thisDir : Path.Combine(pathToNow, thisDir);
var item = Find(root, thisDir);
if (item == null)
{
//AddExistingFolder(root.ContainingProject, pathToNow);
//item = Find(root, thisDir);
//if (item == null)
item = root.AddFolder(thisDir);
}
root = item.ProjectItems;
}
if (Find(root, tmp[tmp.Length - 1]) == null)
root.AddFromFile(Path.Combine(dir, name));
}
示例8: GetFolder
internal static ProjectItem GetFolder(string pathToParent, ProjectItems _projectItems, string folderName, bool createIfNotExists)
{
ProjectItem res = GetProjectItemByName(_projectItems, folderName);
if (res == null)
{
if (createIfNotExists)
{
//folder not in the project, so we try to create one
try
{
res = _projectItems.AddFolder(folderName, EnvDTE.Constants.vsProjectItemKindPhysicalFolder);
}
catch (Exception)
{
//the folder could not be created, maybe the folder exists, but isnot part of the project
string folderpath = Path.Combine(pathToParent, folderName);
if (Directory.Exists(folderpath))
{
//ok, folder is there, lets include it in the project
res = IncludeFolderInProject(pathToParent, _projectItems, folderName);
}
}
}
}
return res;
}
示例9: GetProjectFolder
/// <summary>
/// returns the given folder or creates the folder
/// </summary>
/// <param name="projectItems"></param>
/// <param name="p"></param>
/// <param name="p_3"></param>
/// <returns></returns>
internal static ProjectItem GetProjectFolder(ProjectItems projectItems, string folderName, bool createIfMissing)
{
ProjectItem folderItem = GetProjectItemByName(projectItems, folderName);
if (folderItem == null)
{
if (createIfMissing)
{
folderItem = projectItems.AddFolder(folderName, EnvDTE.Constants.vsProjectItemKindPhysicalFolder);
}
}
return folderItem;
}
示例10: GetOrCreateItem
private ProjectItem GetOrCreateItem(ProjectItems items, string item)
{
ProjectItem parentProjectItem = DteHelperEx.FindItemByName(items, item, true);
if (parentProjectItem == null)
{
parentProjectItem = items.AddFolder(item, SolutionFolderKind);
}
return parentProjectItem;
}
示例11: AddFromItemTypeName
private static void AddFromItemTypeName(Project project, ProjectItems items, string path, string itemTypeName,
NewItemDynamicParameters p, Solution2 sln)
{
if ("folder" == itemTypeName.ToLowerInvariant())
{
if (project.Object is SolutionFolder)
{
var folder = project.Object as SolutionFolder;
folder.AddSolutionFolder(path);
}
else
{
items.AddFolder(path, Constants.vsProjectItemKindPhysicalFolder);
}
}
else
{
if (!itemTypeName.ToLowerInvariant().EndsWith(".zip"))
{
itemTypeName += ".zip";
}
p.Category = GetSafeCategoryValue(p.Category, project.CodeModel);
//todo: validate p.Category against available item/project tmps
if (project.Object is SolutionFolder)
{
SolutionFolder folder = project.Object as SolutionFolder;
NewTemplateItemInSolutionFolder(path, itemTypeName, sln, p, folder);
}
else
{
var t = sln.GetProjectItemTemplate(itemTypeName, p.Category);
items.AddFromTemplate(t, path);
}
}
}