Visual Studio Development Bookmark and Share   
 index > Visual Studio Extensibility > Addin: How to delete or update a file already open in IDE?
 

Addin: How to delete or update a file already open in IDE?

I have a new Addin, it can sometime create a file for the user that already exists, it asks to overwrite if this is the case.

However, if the file is NOT open in the IDE, this works, the Addin deletes the file and then creates a new copy.

But if the file is already being examined in the IDE, this fails.

Does the IDE open the file for exclusive access?

I have opened files before twice in dual instances of VS2008 and I can edit a file that is being viewed in the other instance, as you know VS2008 detects this and tells you it has been modified.

This tells me that it is possible to update a file that is opened, so if the Addin is running what should it do?

I tried opening the existing file in the Addin and doing a Seek(0,Begin) to overwrite it, but this doesnt seem to work.

Is there a general approach to this problem?

Thanks

Cap'n

Captain Kernel  Wednesday, October 14, 2009 10:19 AM
Hi, Captain
I think I think we should consider using the UIHierarchy interface to finish this task, the mainly step to open the projectItem first, and close it then delete it. Now the sample code below will show you how do I delete "Program.cs" projectItem No matter it is open and closed.
                    EnvDTE.UIHierarchy uiHierarchy = (EnvDTE.UIHierarchy)_applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object;
                    UIHierarchyItem hierarchyItem = (UIHierarchyItem)uiHierarchy.UIHierarchyItems.Item(1);
                    //Find the "Programme.cs" item.
                    UIHierarchyItem projectHierarchyItem = hierarchyItem.UIHierarchyItems.Item(1).UIHierarchyItems.Item("Program.cs");
                    //select the "Programme.cs" item.
                    projectHierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect);
                    //open the select item(Programme.cs).
                    uiHierarchy.DoDefaultAction();
                    //close the current open project item.
                    _applicationObject.ExecuteCommand("File.Close", string.Empty);
                    //select the "Programme.cs" item again.
                    projectHierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect);
                    //delete the selected item.
                    ProjectItem projItem = (ProjectItem)projectHierarchyItem.Object;
                    projItem.Delete();
If you have anything unclear, or I misundersood you, feel freee to let me know.
Thanks
Chao
Chao Kuo  Thursday, October 15, 2009 8:38 AM
Hi, Captain
I think I think we should consider using the UIHierarchy interface to finish this task, the mainly step to open the projectItem first, and close it then delete it. Now the sample code below will show you how do I delete "Program.cs" projectItem No matter it is open and closed.
                    EnvDTE.UIHierarchy uiHierarchy = (EnvDTE.UIHierarchy)_applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object;
                    UIHierarchyItem hierarchyItem = (UIHierarchyItem)uiHierarchy.UIHierarchyItems.Item(1);
                    //Find the "Programme.cs" item.
                    UIHierarchyItem projectHierarchyItem = hierarchyItem.UIHierarchyItems.Item(1).UIHierarchyItems.Item("Program.cs");
                    //select the "Programme.cs" item.
                    projectHierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect);
                    //open the select item(Programme.cs).
                    uiHierarchy.DoDefaultAction();
                    //close the current open project item.
                    _applicationObject.ExecuteCommand("File.Close", string.Empty);
                    //select the "Programme.cs" item again.
                    projectHierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect);
                    //delete the selected item.
                    ProjectItem projItem = (ProjectItem)projectHierarchyItem.Object;
                    projItem.Delete();
If you have anything unclear, or I misundersood you, feel freee to let me know.
Thanks
Chao
Chao Kuo  Thursday, October 15, 2009 8:38 AM
Hi, Captain
I think I think we should consider using the UIHierarchy interface to finish this task, the mainly step to open the projectItem first, and close it then delete it. Now the sample code below will show you how do I delete "Program.cs" projectItem No matter it is open and closed.
                    EnvDTE.UIHierarchy uiHierarchy = (EnvDTE.UIHierarchy)_applicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object;

                    UIHierarchyItem hierarchyItem = (UIHierarchyItem)uiHierarchy.UIHierarchyItems.Item(1);

                    //Find the "Programme.cs" item.

                    UIHierarchyItem projectHierarchyItem = hierarchyItem.UIHierarchyItems.Item(1).UIHierarchyItems.Item("Program.cs");

                    //select the "Programme.cs" item.

                    projectHierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect);

                    //open the select item(Programme.cs).

                    uiHierarchy.DoDefaultAction();

                    //close the current open project item.

                    _applicationObject.ExecuteCommand("File.Close", string.Empty);

                    //select the "Programme.cs" item again.

                    projectHierarchyItem.Select(vsUISelectionType.vsUISelectionTypeSelect);

                    //delete the selected item.

                    ProjectItem projItem = (ProjectItem)projectHierarchyItem.Object;

                    projItem.Delete();

If you have anything unclear, or I misundersood you, feel freee to let me know.
Thanks
Chao

Hello Chao

Thanks for the sample code, I'm playing with this now and it looks very promising.

I have one question though, why the need for a hard-coded '1' in the various Item accesses? Is this just how it needs to be, or is that code assuming that users solution has only one project and that the Program.cs file is always first file in the project?

I just need to understand that '1' and I will be able to get this working I think, I dont want to find the code breaks if user has many projects.

Thanks

Cap'n

Captain Kernel  Thursday, October 15, 2009 9:54 AM

Hello, Cap

The structure of UHierarchy is just like a tree, one UIHierarchyItem have many UIHierarchyItems.

I use 1 is to simply programming, 1 means first child of the UIHierarchyItem. Here it means first solution, first project.

Thanks

Chao

Chao Kuo  Thursday, October 15, 2009 10:02 AM

Hello, Cap

The structure of UHierarchy is just like a tree, one UIHierarchyItem have many UIHierarchyItems.

I use 1 is to simply programming, 1 means first child of the UIHierarchyItem. Here it means first solution, first project.

Thanks

Chao


Hi Chao

Thanks for help, I got this working now, it behaves very well (I ended up levergaing .GetItem too, which is a little easier for me).

Finally, my code works fine whether the user has the file open in the IDE or not.

I'd like to re-open the file for them though, if it was already open in the IDE.

So, is there a way to find out if a file in a project is actually open or not, before I do the delete?

Thanks


Cap'n
Captain Kernel  Thursday, October 15, 2009 11:36 AM

Hello, Cap

The structure of UHierarchy is just like a tree, one UIHierarchyItem have many UIHierarchyItems.

I use 1 is to simply programming, 1 means first child of the UIHierarchyItem. Here it means first solution, first project.

Thanks

Chao


OK All is now solved, thanks for your assistance Chao.

Cap'n

Captain Kernel  Thursday, October 15, 2009 9:08 PM

You can use google to search for other answers

Custom Search

More Threads

• Projects hierarchy
• Intercept Verify SQL Syntax command
• Keyboard event capturing
• How do I register icons for custom file types
• How to determine if a project is built
• Search/find a string on a webbrowser kind of custom editor
• VS2005 SP1 Broke Tasklist commenting (TODO, HACK, etc.)
• Project template with relative path in link item adds temp subdir
• AdviseBuildStatusCallback
• How to add a project properties page?