這是粗略一點,但你可以通過這個上輕鬆地做自己:
用法:
string relFilePath = @"MyFolder\output.txt"; // Relative to project root
string filePath = @"C:\output.txt"; // Place of the file
activeProject.ProjectItems.AddExistingFile(relFilePath, filePath);
的擴展方法ProjectItems
:
static class ProjectItemExtender
{
const string folderKindGUID = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
public static ProjectItem AddExistingFile(this ProjectItems projectItemCollection, string relPath, string filePath)
{
string[] path = relPath.Split('\\');
if (path.Length == 0) return null;
Func<ProjectItem, bool> folderPredicate = (item) =>
(item.Kind == folderKindGUID && path.FirstOrDefault() == item.Name);
ProjectItem actItem = projectItemCollection.Cast<ProjectItem>().FirstOrDefault(folderPredicate);
//Create not existing folders on path
if (actItem == null && path.Length > 1)
{
//Could throw an exception on invalid folder name for example...
projectItemCollection.AddFolder(path[0]);
actItem = projectItemCollection.Cast<ProjectItem>().FirstOrDefault(folderPredicate);
}
else if (path.Length == 1)
{
projectItemCollection.AddFromDirectory(filePath);
}
return path.Length == 1 ?
actItem
:
actItem.ProjectItems.AddExistingFile(path.Skip(1).Aggregate((working, next) => working + "\\" + next), filePath);
}
}
所有您需要做的是通過文件夾而不是項目添加文件。
我想知道這個答案。 – Andrew 2012-05-19 05:55:25