Partilhar via


VSProject2.GetUniqueFilename(Object, String, String) Method

Definition

Generates a unique file name within the project. it is used for naming new project items.

public:
 System::String ^ GetUniqueFilename(System::Object ^ pDispatch, System::String ^ bstrRoot, System::String ^ bstrDesiredExt);
public:
 Platform::String ^ GetUniqueFilename(Platform::Object ^ pDispatch, Platform::String ^ bstrRoot, Platform::String ^ bstrDesiredExt);
std::wstring GetUniqueFilename(winrt::Windows::Foundation::IInspectable const & pDispatch, std::wstring const & bstrRoot, std::wstring const & bstrDesiredExt);
[System.Runtime.InteropServices.DispId(16)]
public string GetUniqueFilename (object pDispatch, string bstrRoot, string bstrDesiredExt);
[<System.Runtime.InteropServices.DispId(16)>]
abstract member GetUniqueFilename : obj * string * string -> string
Public Function GetUniqueFilename (pDispatch As Object, bstrRoot As String, bstrDesiredExt As String) As String

Parameters

pDispatch
Object

The name generated has this value as the extension.

bstrRoot
String

A Project object or a folder ProjectItem object. The file name generated is unique within this context.

bstrDesiredExt
String

The name generated starts with this string.

Returns

A string representing a file name that is unique in the project or project folder. If the first parameter is a Project instance, then the name generated is unique within the root project folder. The file name takes the form Root?.Extension, where ? is one or more numerals (1, 2, 3, and so on).

Implements

Attributes

Examples

This example displays a file name that is unique in the project folder, in the form root?.ext. To run this example as an add-in, see How to: Compile and Run the Automation Object Model Code Examples. Open a Visual Basic or Visual C# project before running this example.

[Visual Basic]

' This example displays a file name that is unique in the   
     ' project folder, in the form, root?.ext.  
Public Sub OnConnection(ByVal application As Object,_  
 ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _  
 ByRef custom As Array) Implements IDTExtensibility2.OnConnection  
    applicationObject = CType(application, DTE2)  
    addInInstance = CType(addInInst, AddIn)  
    GetUniqueName(applicationObject)  
End Sub  
Sub GetUniqueName(ByVal dte As DTE2)  
    Dim proj As Project  
    proj = applicationObject.Solution.Projects.Item(1)  
    Dim vsproj As VSLangProj80.VSProject2 = _  
    CType(proj.Object, VSLangProj80.VSProject2)  
    MsgBox(vsproj.GetUniqueFilename(proj, "Root", "ext"))  
End Sub  

[C#]

using System.Windows.Forms;  
using VSLangProj;  
using VSLangProj2;  
using VSLangProj80;  
// This example displays a file name that is unique in the   
// project folder, in the form, root?.ext.  
public void OnConnection(object application,  
 ext_ConnectMode connectMode, object addInInst, ref Array custom)  
{  
    applicationObject = (DTE2)application;  
    addInInstance = (AddIn)addInInst;  
    GetUniqueName((DTE2)applicationObject);  
}  
public void GetUniqueName(DTE2 dte)  
{  
    Project proj = null;  
    proj = applicationObject.Solution.Projects.Item(1);  
    VSLangProj80.VSProject2 vsproj =   
((VSLangProj80.VSProject2)(proj.Object));  
    MessageBox.Show(vsproj.GetUniqueFilename(proj, "Root", "ext"));  
}  

Remarks

A typical use for this method is to generate a name for a new project item.

If you call this method with KeyFile as the root and snk as the extension, the string returned is KeyFile.snk, if the file KeyFile.snk does not already exist. If the file KeyFile.snk already exists, then KeyFile1.snk is returned. Note that if multiple calls are made to this method and no file is created from the returned file name, all the calls return the same value. The results depend only on the files that exist at the time of the method call.

Applies to