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
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.
On Mar 19, 8:10 am, Justin Chase wrote: > 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. 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.
On Mar 19, 8:19 am, B Loggins wrote: > On Mar 19, 8:10 am, Justin Chase wrote: > > > 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. > > 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 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.
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 "B Loggins" a écrit dans le message de news:cfa361b9-2c6f-44d3-8a47-36dbfff8102c@2g2000hsn.googlegroups.com... > 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
On Mar 20, 3:26 pm, "Michel LAPLANE \(MVP\)" wrote: > 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 > "B Loggins" a écrit dans le message denews:cfa361b9-2c6f-44d3-8a47-36dbfff8102c@2g2000hsn.googlegroups.com... > > > 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 Doh! I didn't even think to try that, thanks!