Skip to content

Commit 277ccb0

Browse files
yaira2hishitetsu
andauthored
Code Quality: Fallback to "C:\" when var doesn't work (#15842)
Co-authored-by: hishitetsu <66369541+hishitetsu@users.noreply.github.com>
1 parent 9dc465d commit 277ccb0

File tree

8 files changed

+15
-7
lines changed

8 files changed

+15
-7
lines changed

src/Files.App/Constants.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ public static class UserEnvironmentPaths
240240

241241
public static readonly string RecentItemsPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
242242

243+
public static readonly string SystemDrivePath;
244+
245+
static UserEnvironmentPaths()
246+
{
247+
var systemDrive = Environment.GetEnvironmentVariable("SystemDrive");
248+
SystemDrivePath = !string.IsNullOrEmpty(systemDrive) ? systemDrive : "C:";
249+
}
250+
243251
public static Dictionary<string, string> ShellPlaces =
244252
new()
245253
{

src/Files.App/Data/Items/DriveItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root
239239
IsLocationItem = true,
240240
ShowEjectDevice = item.IsRemovable,
241241
ShowShellItems = true,
242-
ShowFormatDrive = !(item.Type == DriveType.Network || string.Equals(root.Path, $@"{Environment.GetEnvironmentVariable("SystemDrive")}\", StringComparison.OrdinalIgnoreCase)),
242+
ShowFormatDrive = !(item.Type == DriveType.Network || string.Equals(root.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase)),
243243
ShowProperties = true
244244
};
245245
item.Path = string.IsNullOrEmpty(root.Path) ? $"\\\\?\\{root.Name}\\" : root.Path;

src/Files.App/Services/Storage/StorageDevicesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public async IAsyncEnumerable<ILocatableFolder> GetDrivesAsync()
5555

5656
public async Task<ILocatableFolder> GetPrimaryDriveAsync()
5757
{
58-
string cDrivePath = $@"{Environment.GetEnvironmentVariable("SystemDrive")}\";
58+
string cDrivePath = $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\";
5959
return new WindowsStorageFolder(await StorageFolder.GetFolderFromPathAsync(cDrivePath));
6060
}
6161

src/Files.App/Utils/Cloud/Detector/LucidLinkCloudDetector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override async IAsyncEnumerable<ICloudProvider> GetProviders()
2828
string syncFolder = inner.GetProperty("filespaceName").GetString();
2929

3030
string[] orgNameFilespaceName = syncFolder.Split(".");
31-
string path = Path.Combine($@"{Environment.GetEnvironmentVariable("SystemDrive")}\Volumes", orgNameFilespaceName[1], orgNameFilespaceName[0]);
31+
string path = Path.Combine($@"{Constants.UserEnvironmentPaths.SystemDrivePath}\Volumes", orgNameFilespaceName[1], orgNameFilespaceName[0]);
3232
string filespaceName = orgNameFilespaceName[0];
3333

3434
string iconPath = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles"), "Lucid", "resources", "Logo.ico");

src/Files.App/Utils/Shell/ContextMenu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public async Task<bool> InvokeItem(int itemID)
174174

175175
public static async Task WarmUpQueryContextMenuAsync()
176176
{
177-
using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Environment.GetEnvironmentVariable("SystemDrive")}\" }, Shell32.CMF.CMF_NORMAL);
177+
using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, Shell32.CMF.CMF_NORMAL);
178178
}
179179

180180
private void EnumMenuItems(HMENU hMenu, List<Win32ContextMenuItem> menuItemsResult, bool loadSubenus = false)

src/Files.App/Utils/Storage/Helpers/StorageSenseHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal sealed class StorageSenseHelper
1010
{
1111
public static async Task OpenStorageSenseAsync(string path)
1212
{
13-
if (!path.StartsWith(Environment.GetEnvironmentVariable("SystemDrive"), StringComparison.OrdinalIgnoreCase)
13+
if (!path.StartsWith(Constants.UserEnvironmentPaths.SystemDrivePath, StringComparison.OrdinalIgnoreCase)
1414
&& ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
1515
{
1616
LaunchHelper.LaunchSettings("page=SettingsPageStorageSenseStorageOverview&target=SystemSettings_StorageSense_VolumeListLink");

src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class CreateShortcutDialogViewModel : ObservableObject
1515
public readonly string WorkingDirectory;
1616

1717
// Placeholder text of destination path textbox
18-
public readonly string DestinationPlaceholder = $@"{Environment.GetEnvironmentVariable("SystemDrive")}\Users\";
18+
public readonly string DestinationPlaceholder = $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\Users\";
1919

2020
// Tells whether destination path exists
2121
public bool DestinationPathExists { get; set; }

src/Files.App/ViewModels/Properties/BasePropertiesPage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
4040
BaseProperties = props;
4141

4242
ViewModel.CleanupVisibility = props.Drive.Type != DriveType.Network;
43-
ViewModel.FormatVisibility = !(props.Drive.Type == DriveType.Network || string.Equals(props.Drive.Path, $@"{Environment.GetEnvironmentVariable("SystemDrive")}\", StringComparison.OrdinalIgnoreCase));
43+
ViewModel.FormatVisibility = !(props.Drive.Type == DriveType.Network || string.Equals(props.Drive.Path, $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\", StringComparison.OrdinalIgnoreCase));
4444
ViewModel.CleanupDriveCommand = new AsyncRelayCommand(() => StorageSenseHelper.OpenStorageSenseAsync(props.Drive.Path));
4545
ViewModel.FormatDriveCommand = new RelayCommand(async () =>
4646
{

0 commit comments

Comments
 (0)