Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / Extensibility / March 2008

Tip: Looking for answers? Try searching our database.

How do you rename project items programmatically?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
B Loggins - 19 Mar 2008 04:26 GMT
Let's say I have a project with file "c:\foo\bar.cs" and I want to
point that project item to a new name ("c:\bar\baz.cs").  I don't want
to use SaveAs, because the new file may already exist.

One way to do that is to remove the old item and add the new one, but
that has some undesirable effects.  Is there any way to do this using
the SDK or Automation objects?

Thanks!
Breckin
Justin Chase - 19 Mar 2008 14:10 GMT
Well all you're doing is basically renaming a file and if the file
already exists then no matter the method you choose you're going to
have the same problems. You either prompt the user to overwrite
"baz.cs" or you would give it some name such as "baz2.cs". If you want
to know if the file already exists just use some code such as:

if(File.Exists(path))
  path = Path.GetDirectoryName(path) + "baz2.cs";

SaveAs(path);

Alternately you could prompt the user to overwrite the other file
instead.
B Loggins - 19 Mar 2008 15:19 GMT
> Well all you're doing is basically renaming a file and if the file
> already exists then no matter the method you choose you're going to
[quoted text clipped - 9 lines]
> Alternately you could prompt the user to overwrite the other file
> instead.

True.  I guess what I was looking for was a method to have a project
item "point" to another file that already exists on disk.  I know it's
a weird workflow, just seeing if it's possible.
Justin Chase - 20 Mar 2008 14:37 GMT
> > Well all you're doing is basically renaming a file and if the file
> > already exists then no matter the method you choose you're going to
[quoted text clipped - 13 lines]
> item "point" to another file that already exists on disk.  I know it's
> a weird workflow, just seeing if it's possible

You could read the contents of the new one, write it to the old one
then save it.

You could add the new one, remove the old one, then rename the new
one.

Those are some other ideas.
Michel LAPLANE (MVP) - 20 Mar 2008 21:26 GMT
Just use a macro or addin to get the projectitem and change the property
name, Visual studio do all the work :
   in C#
   //get the project in the projects collection of the solution using a
Solution2 object
   solution.Projects
   // Get the projectitem in the ProjectItems collection of the project
using a Project object
   project.ProjectItems
   // change the property Name of the ProjectItem Object
   projectItem.Name = "NewName"
   // Vs change the item name and the file name automatically

Enjoy with VS
> Let's say I have a project with file "c:\foo\bar.cs" and I want to
> point that project item to a new name ("c:\bar\baz.cs").  I don't want
[quoted text clipped - 6 lines]
> Thanks!
> Breckin
B Loggins - 21 Mar 2008 03:38 GMT
On Mar 20, 3:26 pm, "Michel LAPLANE \(MVP\)" <mich.lapl...@wanadoo.fr>
wrote:
> Just use a macro or addin to get the projectitem and change the property
> name, Visual studio do all the work :
[quoted text clipped - 21 lines]
> > Thanks!
> > Breckin

Doh!  I didn't even think to try that, thanks!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.