protected virtual void RenameProjectFile(string newFile)
{
var shell = Site.GetService(typeof (SVsUIShell)) as IVsUIShell;
Debug.Assert(shell != null, "Could not get the ui shell from the project");
if (shell == null)
{
throw new InvalidOperationException();
}
// Do some name validation
if (Utilities.ContainsInvalidFileNameChars(newFile))
{
throw new InvalidOperationException(SR.GetString(SR.ErrorInvalidProjectName,
CultureInfo.CurrentUICulture));
}
// Figure out what the new full name is
var oldFile = Url;
var canContinue = 0;
var vsSolution = (IVsSolution) GetService(typeof (SVsSolution));
if (ErrorHandler.Succeeded(vsSolution.QueryRenameProject(InteropSafeIVsProject3, oldFile, newFile, 0,
out canContinue))
&& canContinue != 0)
{
var isFileSame = NativeMethods.IsSamePath(oldFile, newFile);
// If file already exist and is not the same file with different casing
if (!isFileSame && File.Exists(newFile))
{
// Prompt the user for replace
var message = SR.GetString(SR.FileAlreadyExists, newFile);
if (!Utilities.IsInAutomationFunction(Site))
{
if (!VsShellUtilities.PromptYesNo(message, null, OLEMSGICON.OLEMSGICON_WARNING, shell))
{
throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
}
}
else
{
throw new InvalidOperationException(message);
}
// Delete the destination file after making sure it is not read only
File.SetAttributes(newFile, FileAttributes.Normal);
File.Delete(newFile);
}
var fileChanges = new SuspendFileChanges(Site, FileName);
fileChanges.Suspend();
try
{
// Actual file rename
SaveMSBuildProjectFileAs(newFile);
SetProjectFileDirty(false);
if (!isFileSame)
{
// Now that the new file name has been created delete the old one.
// TODO: Handle source control issues.
File.SetAttributes(oldFile, FileAttributes.Normal);
File.Delete(oldFile);
}
OnPropertyChanged(this, (int) __VSHPROPID.VSHPROPID_Caption, 0);
// Update solution
ErrorHandler.ThrowOnFailure(vsSolution.OnAfterRenameProject(InteropSafeIVsProject3, oldFile, newFile,
0));
ErrorHandler.ThrowOnFailure(shell.RefreshPropertyBrowser(0));
}
finally
{
fileChanges.Resume();
}
}
else
{
throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
}
}