Freigeben über


VSProject2.GetUniqueFilename Method

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

Namespace:  VSLangProj80
Assembly:  VSLangProj80 (in VSLangProj80.dll)

Syntax

'Declaration
Function GetUniqueFilename ( _
    pDispatch As Object, _
    bstrRoot As String, _
    bstrDesiredExt As String _
) As String
string GetUniqueFilename(
    Object pDispatch,
    string bstrRoot,
    string bstrDesiredExt
)
String^ GetUniqueFilename(
    [InAttribute] Object^ pDispatch, 
    [InAttribute] String^ bstrRoot, 
    [InAttribute] String^ bstrDesiredExt
)
abstract GetUniqueFilename : 
        pDispatch:Object * 
        bstrRoot:string * 
        bstrDesiredExt:string -> string
function GetUniqueFilename(
    pDispatch : Object, 
    bstrRoot : String, 
    bstrDesiredExt : String
) : String

Parameters

  • pDispatch
    Type: System.Object

    The name generated has this value as the extension.

  • bstrDesiredExt
    Type: System.String

    The name generated starts with this string.

Return Value

Type: System.String
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).

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.

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"));
}

.NET Framework Security

See Also

Reference

VSProject2 Interface

VSLangProj80 Namespace