You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Reference the name or location of the project file
2
+
title: Reference the project file name or location
3
3
description: Use MSBuild reserved properties to reference project file name or location without having to create your own properties.
4
-
ms.date: 11/04/2016
4
+
ms.date: 04/29/2025
5
5
ms.topic: how-to
6
6
helpviewer_keywords:
7
7
- locations, referencing
@@ -15,33 +15,37 @@ author: ghogen
15
15
ms.author: ghogen
16
16
manager: mijacobs
17
17
ms.subservice: msbuild
18
+
19
+
#customer intent: As a builder, I want to reference reserved properties like the project file name in builds so I don't have to create the properties.
18
20
---
19
-
# Reference the name or location of the project file
21
+
# Use reserved properties to reference project file name or location
20
22
21
-
You can use the name or location of the project in the project file itself without having to create your own property. MSBuild provides reserved properties that reference the project file name and other properties related to the project. For more information on reserved properties, see [MSBuild reserved and well-known properties](../msbuild/msbuild-reserved-and-well-known-properties.md).
23
+
MSBuild provides reserved properties that reference the project file name and other properties related to the project. You can use the name or location of the project in the project file itself without having to create your own property. For more information on reserved properties, see [MSBuild reserved and well-known properties](msbuild-reserved-and-well-known-properties.md).
22
24
23
-
## Use the project properties
25
+
## Prerequisites
24
26
25
-
MSBuild provides some reserved properties that you can use in your project files without defining them each time. For example, the reserved property `MSBuildProjectName` provides a reference to the project file name. The reserved property `MSBuildProjectDirectory` provides a reference to the project file location.
27
+
A Visual Studio project that builds with MSBuild.
26
28
27
-
#### To use the project properties
29
+
##Use reserved project properties
28
30
29
-
- Reference the property in the project file with the $() notation, just as you would with any property. For example:
31
+
MSBuild provides some reserved properties that you can use in your project files without defining them each time. For example, the reserved property `MSBuildProjectName` provides a reference to the project file name. The reserved property `MSBuildProjectDirectory` provides a reference to the project file location.
30
32
31
-
```xml
32
-
<CSC Sources = "@(CSFile)"
33
-
OutputAssembly = "$(MSBuildProjectName).exe"/>
34
-
</CSC>
35
-
```
33
+
An advantage of using the reserved property is that any changes to the project file name are incorporated automatically. The next time you build the project, the output file and other file names that use the property automatically update to the new name.
36
34
37
-
An advantage of using a reserved property is that any changes to the project file name are incorporated automatically. The next time that you build the project, the output file will have the new name with no further action required on your part.
35
+
To use the project properties, reference the property in the projectfile with the `$()` notation, just as you would any property. For example:
38
36
39
-
For more info on the use of special characters in file or project references, see [MSBuild special characters](../msbuild/msbuild-special-characters.md).
37
+
```xml
38
+
<CSC Sources = "@(CSFile)"
39
+
OutputAssembly = "$(MSBuildProjectName).exe"/>
40
+
</CSC>
41
+
```
42
+
43
+
For information about using special characters in file or project references, see [MSBuild special characters](msbuild-special-characters.md).
40
44
41
45
> [!NOTE]
42
-
> Reserved properties cannot be redefined in the project file.
46
+
> You can't redefine reserved properties in the project file.
43
47
44
-
## Example 1
48
+
## Use MSBuildProjectName to specify output file name
45
49
46
50
The following example project file references the project name as a reserved property to specify the name for the output.
47
51
@@ -52,7 +56,7 @@ You can use the name or location of the project in the project file itself witho
52
56
<!-- Specify the inputs -->
53
57
<ItemGroup>
54
58
<CSFile Include = "consolehwcs1.cs"/>
55
-
</ItemGroup>
59
+
</ItemGroup>
56
60
<Target Name = "Compile">
57
61
<!-- Run the Visual C# compilation using
58
62
input files of type CSFile -->
@@ -70,9 +74,9 @@ You can use the name or location of the project in the project file itself witho
70
74
</Project>
71
75
```
72
76
73
-
## Example 2
77
+
## Use MSBuildProjectDirectory to create the full path to a file
74
78
75
-
The following example project file uses the `MSBuildProjectDirectory` reserved property to create the full path to a file in the project file location.
79
+
The following example project file uses the `MSBuildProjectDirectory` reserved property to create the full path to a file in the project file location. The example uses the [property function syntax](property-functions.md#property-function-syntax) to call the static .NET Framework method <xref:System.IO.Path.Combine*?displayProperty=fullName>.
@@ -84,9 +88,7 @@ You can use the name or location of the project in the project file itself witho
84
88
</Project>
85
89
```
86
90
87
-
The example uses the [Property function](property-functions.md) syntax to call the static .NET Framework method <xref:System.IO.Path.Combine*?displayProperty=fullName>.
88
-
89
91
## Related content
90
92
91
-
-[MSBuild](../msbuild/msbuild.md)
92
-
-[MSBuild reserved and well-known properties](../msbuild/msbuild-reserved-and-well-known-properties.md)
93
+
-[MSBuild](msbuild.md)
94
+
-[MSBuild reserved and well-known properties](msbuild-reserved-and-well-known-properties.md)
0 commit comments