5

Visual Studio 2008 の拡張機能を使用して、インターフェイスの解析後にさまざまなメッセージを含むプロジェクト フォルダーを作成するアドインを作成しようとしています。ただし、フォルダーの作成/追加の段階で問題があります。使ってみました

ProjectItem folder = 
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty); 

(アイテムは、同じ名前で「メッセージ」が追加されたフォルダーを作成している隣のターゲットファイルです)が、フォルダーが既に存在する場合はチョークします(大きな驚きはありません)。

次のように、既に存在する場合は削除してみました。

DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent + 
newDirectoryName); 
if (dirInfo.Exists) 
{
    dirInfo.Delete(true);
}

ProjectItem folder = 
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty); 

デバッグ中にフォルダーが削除されることがわかりますが、それでもフォルダーがまだそこにあり、フォルダーが既に存在するという例外で死ぬと考えているようです。

何か案は???

ありがとう。

AK

....おそらく答えは、削除後にプロジェクトをプログラムで更新することにあるでしょうか? これはどのように行うことができますか?

4

5 に答える 5

4
ProjectItem pi = null;
var dir = Path.Combine(
      project.Properties.Item("LocalPath").Value.ToString(), SubdirectoryName);
if (Directory.Exists(dir))
    pi = target.ProjectItems.AddFromDirectory(dir);
else
    pi = target.ProjectItems.AddFolder(dir);

ProjectItems.AddFromDirectoryは、ディレクトリとそのディレクトリの下にあるすべてのものをプロジェクトに追加します。

于 2011-04-07T19:25:59.057 に答える
3

うん、そうだった…

DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent + newDirectoryName);

if (dirInfo.Exists)
{
    dirInfo.Delete(true);
    item.DTE.ExecuteCommand("View.Refresh", string.Empty);
}

ProjectItem folder = item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);

これを行うためのよりエレガントな方法があれば、それは大歓迎です...

ありがとう。

于 2008-09-15T17:25:52.823 に答える
2

これが私のアプローチです:

//Getting the current project
private DTE2 _applicationObject;
System.Array projs = (System.Array)_applicationObject.ActiveSolutionProjects;
Project proy=(Project)projs.GetValue(0);
//Getting the path
string path=proy.FullName.Substring(0,proy.FullName.LastIndexOf('\\'));
//Valitating if the path exists
bool existsDirectory= Directory.Exists(path + "\\Directory");
//Deleting and creating the Directory
if (existeClasses)
   Directory.Delete(path + "\\Directory", true);
Directory.CreateDirectory(path + "\\Directory");
//Including in the project
proy.ProjectItems.AddFromDirectory(path + "\\Directory");
于 2011-06-28T20:54:38.227 に答える
0

これは、NAnt を長い間使用しており、うまくいくかもしれないと考えていたので、私が考えたアイデアです。

テキスト エディターで .csproj ファイルを開き、次のようなディレクトリを追加します。

<ItemGroup>
   <compile include="\path\rootFolderToInclude\**\*.cs" />
</ItemGroup>

「ItemGroup」がすでに存在する場合は問題ありません。既存のものに追加するだけです。Visual Studio はこのエントリを編集する方法を実際には知りませんが、ディレクトリ全体をスキャンします。

好きなように編集します。

于 2009-07-30T20:59:06.240 に答える