|
| 1 | +--- |
| 2 | +title: ".NET 7 breaking change: Time fields on symbolic links" |
| 3 | +description: Learn about the .NET 7 breaking change in core .NET libraries where updating CreationTime[Utc], LastAccessTime[Utc], and LastWriteTime[Utc] on a symbolic link no longer affects the target . |
| 4 | +ms.date: 10/04/2022 |
| 5 | +--- |
| 6 | +# Time fields on symbolic links |
| 7 | + |
| 8 | +When changes are made to the following time-related fields on a symbolic link ("symlink"), the updates now affect the symlink itself and not the target: |
| 9 | + |
| 10 | +- <xref:System.IO.FileSystemInfo.CreationTime> |
| 11 | +- <xref:System.IO.FileSystemInfo.CreationTimeUtc> |
| 12 | +- <xref:System.IO.FileSystemInfo.LastAccessTime> |
| 13 | +- <xref:System.IO.FileSystemInfo.LastAccessTimeUtc> |
| 14 | +- <xref:System.IO.FileSystemInfo.LastWriteTime> |
| 15 | +- <xref:System.IO.FileSystemInfo.LastWriteTimeUtc> |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +Previously, updating any of the time-related fields on a symlink affected the fields of the symlink's target. |
| 20 | + |
| 21 | +Consider the following program that prints the various time field values on a file and its symbolic link, updates the symlink's time field values to 1 day later, and then reprints the time field values on both the file and symlink. |
| 22 | + |
| 23 | +```csharp |
| 24 | +string filename = "file"; |
| 25 | +string linkname = "link"; |
| 26 | + |
| 27 | +// Create a file and symlink. |
| 28 | +File.Create(filename).Dispose(); |
| 29 | +File.CreateSymbolicLink(linkname, filename); |
| 30 | + |
| 31 | +Console.WriteLine("Before update:"); |
| 32 | +PrintMetadata(filename); |
| 33 | +PrintMetadata(linkname); |
| 34 | + |
| 35 | +UpdateMetadata(linkname); |
| 36 | + |
| 37 | +Console.WriteLine("\nAfter update:"); |
| 38 | +PrintMetadata(filename); |
| 39 | +PrintMetadata(linkname); |
| 40 | + |
| 41 | +static void UpdateMetadata(string filename) |
| 42 | +{ |
| 43 | + DateTime tomorrow = DateTime.Now.AddDays(1); |
| 44 | + |
| 45 | + File.SetCreationTime(filename, tomorrow); |
| 46 | + File.SetLastAccessTime(filename, tomorrow); |
| 47 | + File.SetLastWriteTime(filename, tomorrow); |
| 48 | + File.SetAttributes(filename, File.GetAttributes(filename) | FileAttributes.Offline); |
| 49 | +} |
| 50 | + |
| 51 | +static void PrintMetadata(string filename) |
| 52 | +{ |
| 53 | + Console.WriteLine($"---{filename}---"); |
| 54 | + Console.WriteLine("Creation:\t" + File.GetCreationTime(filename)); |
| 55 | + Console.WriteLine("Last access:\t" + File.GetLastAccessTime(filename)); |
| 56 | + Console.WriteLine("Last write:\t" + File.GetLastWriteTime(filename)); |
| 57 | + Console.WriteLine("Attributes:\t" + File.GetAttributes(filename)); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +Previously, after updating the values on the symlink, only the target file's time fields were updated. The output of the preceding program was as follows: |
| 62 | + |
| 63 | +```output |
| 64 | +Before update: |
| 65 | +---file--- |
| 66 | +Creation: 9/29/2022 10:35:40 AM |
| 67 | +Last access: 9/29/2022 10:35:40 AM |
| 68 | +Last write: 9/29/2022 10:35:40 AM |
| 69 | +Attributes: Archive |
| 70 | +---link--- |
| 71 | +Creation: 9/29/2022 10:35:40 AM |
| 72 | +Last access: 9/29/2022 10:35:40 AM |
| 73 | +Last write: 9/29/2022 10:35:40 AM |
| 74 | +Attributes: Archive, ReparsePoint |
| 75 | +
|
| 76 | +After update: |
| 77 | +---file--- |
| 78 | +Creation: 9/30/2022 10:35:40 AM |
| 79 | +Last access: 9/30/2022 10:35:40 AM |
| 80 | +Last write: 9/30/2022 10:35:40 AM |
| 81 | +Attributes: Archive |
| 82 | +---link--- |
| 83 | +Creation: 9/29/2022 10:35:40 AM |
| 84 | +Last access: 9/29/2022 10:35:40 AM |
| 85 | +Last write: 9/29/2022 10:35:40 AM |
| 86 | +Attributes: Archive, ReparsePoint, Offline |
| 87 | +``` |
| 88 | + |
| 89 | +## New behavior |
| 90 | + |
| 91 | +Starting in .NET 7, updating any of the time-related fields on a symlink affects the fields of the symlink itself and not the target file. |
| 92 | + |
| 93 | +The output of the program shown in the [Previous behavior](#previous-behavior) section is as follows: |
| 94 | + |
| 95 | +```output |
| 96 | +Before update: |
| 97 | +---file--- |
| 98 | +Creation: 9/29/2022 10:33:39 AM |
| 99 | +Last access: 9/29/2022 10:33:39 AM |
| 100 | +Last write: 9/29/2022 10:33:39 AM |
| 101 | +Attributes: Archive |
| 102 | +---link--- |
| 103 | +Creation: 9/29/2022 10:33:39 AM |
| 104 | +Last access: 9/29/2022 10:33:39 AM |
| 105 | +Last write: 9/29/2022 10:33:39 AM |
| 106 | +Attributes: Archive, ReparsePoint |
| 107 | +
|
| 108 | +After update: |
| 109 | +---file--- |
| 110 | +Creation: 9/29/2022 10:33:39 AM |
| 111 | +Last access: 9/29/2022 10:33:39 AM |
| 112 | +Last write: 9/29/2022 10:33:39 AM |
| 113 | +Attributes: Archive |
| 114 | +---link--- |
| 115 | +Creation: 9/30/2022 10:33:39 AM |
| 116 | +Last access: 9/30/2022 10:33:39 AM |
| 117 | +Last write: 9/30/2022 10:33:39 AM |
| 118 | +Attributes: Archive, ReparsePoint, Offline |
| 119 | +``` |
| 120 | + |
| 121 | +## Version introduced |
| 122 | + |
| 123 | +.NET 7 Preview 1 |
| 124 | + |
| 125 | +## Type of breaking change |
| 126 | + |
| 127 | +This change can affect [binary compatibility](../../categories.md#binary-compatibility). |
| 128 | + |
| 129 | +## Reason for change |
| 130 | + |
| 131 | +The previous behavior was unexpected and undesirable in some cases: |
| 132 | + |
| 133 | +- It was inconsistent with the behavior of the properties and methods that get the same fields. |
| 134 | +- It was also impossible to actually update the fields in the symlink itself using .NET APIs. |
| 135 | + |
| 136 | +## Recommended action |
| 137 | + |
| 138 | +If you were relying on this behavior to set values on the symlink's target, setting one of the `*Time` fields in a symlink will no longer affect the target. You can use the new symbolic link APIs to obtain the target of a symlink and then update that file system object instead. |
| 139 | + |
| 140 | +```csharp |
| 141 | +FileSystemInfo? targetInfo = linkInfo.ResolveLinkTarget(returnFinalTarget: true); |
| 142 | +if (targetInfo != null) |
| 143 | +{ |
| 144 | + // Update the properties accordingly. |
| 145 | + targetInfo.LastWriteTime = DateTime.Now; |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## Affected APIs |
| 150 | + |
| 151 | +- <xref:System.IO.Directory.SetCreationTime(System.String,System.DateTime)?displayProperty=fullName> |
| 152 | +- <xref:System.IO.Directory.SetCreationTimeUtc(System.String,System.DateTime)?displayProperty=fullName> |
| 153 | +- <xref:System.IO.Directory.SetLastAccessTime(System.String,System.DateTime)?displayProperty=fullName> |
| 154 | +- <xref:System.IO.Directory.SetLastAccessTimeUtc(System.String,System.DateTime)?displayProperty=fullName> |
| 155 | +- <xref:System.IO.Directory.SetLastWriteTime(System.String,System.DateTime)?displayProperty=fullName> |
| 156 | +- <xref:System.IO.Directory.SetLastWriteTimeUtc(System.String,System.DateTime)?displayProperty=fullName> |
| 157 | +- <xref:System.IO.File.SetCreationTime(System.String,System.DateTime)?displayProperty=fullName> |
| 158 | +- <xref:System.IO.File.SetCreationTimeUtc(System.String,System.DateTime)?displayProperty=fullName> |
| 159 | +- <xref:System.IO.File.SetLastAccessTime(System.String,System.DateTime)?displayProperty=fullName> |
| 160 | +- <xref:System.IO.File.SetLastAccessTimeUtc(System.String,System.DateTime)?displayProperty=fullName> |
| 161 | +- <xref:System.IO.File.SetLastWriteTime(System.String,System.DateTime)?displayProperty=fullName> |
| 162 | +- <xref:System.IO.File.SetLastWriteTimeUtc(System.String,System.DateTime)?displayProperty=fullName> |
| 163 | +- <xref:System.IO.FileSystemInfo.CreationTime?displayProperty=fullName> |
| 164 | +- <xref:System.IO.FileSystemInfo.CreationTimeUtc?displayProperty=fullName> |
| 165 | +- <xref:System.IO.FileSystemInfo.LastAccessTime?displayProperty=fullName> |
| 166 | +- <xref:System.IO.FileSystemInfo.LastAccessTimeUtc?displayProperty=fullName> |
| 167 | +- <xref:System.IO.FileSystemInfo.LastWriteTime?displayProperty=fullName> |
| 168 | +- <xref:System.IO.FileSystemInfo.LastWriteTimeUtc?displayProperty=fullName> |
0 commit comments