Skip to content

Commit dc8f13c

Browse files
committed
Added functions to enable and disable winget admin settings
1 parent d6108b4 commit dc8f13c

4 files changed

Lines changed: 471 additions & 0 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ private protected ProcessOutputReader OutputReader
3434
private const string _infoCmd = "--info";
3535
private const string _versionCmd = "--version";
3636
private const string _exportSettingsCmd = "settings export";
37+
private const string _settingsEnableCmd = "settings --enable \"{0}\"";
38+
private const string _settingsDisableCmd = "settings --disable \"{0}\"";
3739

3840
private ProcessManager _processManager;
3941
private string _wingetExePath;
@@ -130,6 +132,7 @@ public WinGet()
130132
QueryInstallation();
131133
}
132134

135+
//---Settings Export---------------------------------------------------------------------------
133136
/// <summary>
134137
/// Exports the WinGet settings to a json string.
135138
/// </summary>
@@ -258,7 +261,255 @@ public async Task ExportSettingsToFileAsync(string file)
258261

259262
await FileHelper.WriteTextToFileAsync(file, OutputReader.ExportOutputToString(result));
260263
}
264+
//---------------------------------------------------------------------------------------------
261265

266+
//---Manage Settings---------------------------------------------------------------------------
267+
/// <summary>
268+
/// Enables the provided admin setting (Needs administrator rights).
269+
/// </summary>
270+
/// <param name="settingName">
271+
/// Name of the admin setting to enable.
272+
/// </param>
273+
/// <returns>
274+
/// <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
275+
/// </returns>
276+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
277+
/// WinGet is not installed or not found on the system.
278+
/// </exception>
279+
/// <exception cref="System.ArgumentNullException">
280+
/// A provided argument is null.
281+
/// </exception>
282+
/// <exception cref="System.ArgumentException">
283+
/// A provided argument is empty.
284+
/// </exception>
285+
/// <exception cref="System.Security.SecurityException">
286+
/// The current user is missing administrator privileges for this call.
287+
/// </exception>
288+
public bool EnableAdminSetting(string settingName)
289+
{
290+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
291+
292+
string cmd = string.Format(_settingsEnableCmd, settingName);
293+
294+
ProcessResult result = Execute(cmd, true);
295+
296+
return result.Success;
297+
}
298+
299+
/// <summary>
300+
/// Enables the provided admin setting (Needs administrator rights).
301+
/// </summary>
302+
/// <param name="setting">
303+
/// The <see cref="WGetNET.WinGetAdminOption"/> to enable.
304+
/// </param>
305+
/// <returns>
306+
/// <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
307+
/// </returns>
308+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
309+
/// WinGet is not installed or not found on the system.
310+
/// </exception>
311+
/// <exception cref="System.ArgumentNullException">
312+
/// A provided argument is null.
313+
/// </exception>
314+
/// <exception cref="System.ArgumentException">
315+
/// A provided argument is empty.
316+
/// </exception>
317+
/// <exception cref="System.Security.SecurityException">
318+
/// The current user is missing administrator privileges for this call.
319+
/// </exception>
320+
public bool EnableAdminSetting(WinGetAdminOption setting)
321+
{
322+
ArgsHelper.ThrowIfObjectIsNull(setting, "setting");
323+
324+
return EnableAdminSetting(setting.EntryName);
325+
}
326+
327+
/// <summary>
328+
/// Asynchronously enables the provided admin setting (Needs administrator rights).
329+
/// </summary>
330+
/// <param name="settingName">
331+
/// Name of the admin setting to enable.
332+
/// </param>
333+
/// <returns>
334+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
335+
/// The result is <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
336+
/// </returns>
337+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
338+
/// WinGet is not installed or not found on the system.
339+
/// </exception>
340+
/// <exception cref="System.ArgumentNullException">
341+
/// A provided argument is null.
342+
/// </exception>
343+
/// <exception cref="System.ArgumentException">
344+
/// A provided argument is empty.
345+
/// </exception>
346+
/// <exception cref="System.Security.SecurityException">
347+
/// The current user is missing administrator privileges for this call.
348+
/// </exception>
349+
public async Task<bool> EnableAdminSettingAsync(string settingName)
350+
{
351+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
352+
353+
string cmd = string.Format(_settingsEnableCmd, settingName);
354+
355+
ProcessResult result = await ExecuteAsync(cmd, true);
356+
357+
return result.Success;
358+
}
359+
360+
/// <summary>
361+
/// Asynchronously enables the provided admin setting (Needs administrator rights).
362+
/// </summary>
363+
/// <param name="setting">
364+
/// The <see cref="WGetNET.WinGetAdminOption"/> to enable.
365+
/// </param>
366+
/// <returns>
367+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
368+
/// The result is <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
369+
/// </returns>
370+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
371+
/// WinGet is not installed or not found on the system.
372+
/// </exception>
373+
/// <exception cref="System.ArgumentNullException">
374+
/// A provided argument is null.
375+
/// </exception>
376+
/// <exception cref="System.ArgumentException">
377+
/// A provided argument is empty.
378+
/// </exception>
379+
/// <exception cref="System.Security.SecurityException">
380+
/// The current user is missing administrator privileges for this call.
381+
/// </exception>
382+
public async Task<bool> EnableAdminSettingAsynv(WinGetAdminOption setting)
383+
{
384+
ArgsHelper.ThrowIfObjectIsNull(setting, "setting");
385+
386+
return await EnableAdminSettingAsync(setting.EntryName);
387+
}
388+
389+
/// <summary>
390+
/// Disables the provided admin setting (Needs administrator rights).
391+
/// </summary>
392+
/// <param name="settingName">
393+
/// Name of the admin setting to disable.
394+
/// </param>
395+
/// <returns>
396+
/// <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
397+
/// </returns>
398+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
399+
/// WinGet is not installed or not found on the system.
400+
/// </exception>
401+
/// <exception cref="System.ArgumentNullException">
402+
/// A provided argument is null.
403+
/// </exception>
404+
/// <exception cref="System.ArgumentException">
405+
/// A provided argument is empty.
406+
/// </exception>
407+
/// <exception cref="System.Security.SecurityException">
408+
/// The current user is missing administrator privileges for this call.
409+
/// </exception>
410+
public bool DisableAdminSetting(string settingName)
411+
{
412+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
413+
414+
string cmd = string.Format(_settingsDisableCmd, settingName);
415+
416+
ProcessResult result = Execute(cmd, true);
417+
418+
return result.Success;
419+
}
420+
421+
/// <summary>
422+
/// Disables the provided admin setting (Needs administrator rights).
423+
/// </summary>
424+
/// <param name="setting">
425+
/// The <see cref="WGetNET.WinGetAdminOption"/> to disable.
426+
/// </param>
427+
/// <returns>
428+
/// <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
429+
/// </returns>
430+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
431+
/// WinGet is not installed or not found on the system.
432+
/// </exception>
433+
/// <exception cref="System.ArgumentNullException">
434+
/// A provided argument is null.
435+
/// </exception>
436+
/// <exception cref="System.ArgumentException">
437+
/// A provided argument is empty.
438+
/// </exception>
439+
/// <exception cref="System.Security.SecurityException">
440+
/// The current user is missing administrator privileges for this call.
441+
/// </exception>
442+
public bool DisableAdminSetting(WinGetAdminOption setting)
443+
{
444+
ArgsHelper.ThrowIfObjectIsNull(setting, "setting");
445+
446+
return DisableAdminSetting(setting.EntryName);
447+
}
448+
449+
/// <summary>
450+
/// Asynchronously disables the provided admin setting (Needs administrator rights).
451+
/// </summary>
452+
/// <param name="settingName">
453+
/// Name of the admin setting to disable.
454+
/// </param>
455+
/// <returns>
456+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
457+
/// The result is <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
458+
/// </returns>
459+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
460+
/// WinGet is not installed or not found on the system.
461+
/// </exception>
462+
/// <exception cref="System.ArgumentNullException">
463+
/// A provided argument is null.
464+
/// </exception>
465+
/// <exception cref="System.ArgumentException">
466+
/// A provided argument is empty.
467+
/// </exception>
468+
/// <exception cref="System.Security.SecurityException">
469+
/// The current user is missing administrator privileges for this call.
470+
/// </exception>
471+
public async Task<bool> DisableAdminSettingAsync(string settingName)
472+
{
473+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
474+
475+
string cmd = string.Format(_settingsDisableCmd, settingName);
476+
477+
ProcessResult result = await ExecuteAsync(cmd, true);
478+
479+
return result.Success;
480+
}
481+
482+
/// <summary>
483+
/// Asynchronously disables the provided admin setting (Needs administrator rights).
484+
/// </summary>
485+
/// <param name="setting">
486+
/// The <see cref="WGetNET.WinGetAdminOption"/> to disable.
487+
/// </param>
488+
/// <returns>
489+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
490+
/// The result is <see langword="true"/> if the action was succesfull and <see langword="false"/> if it failed.
491+
/// </returns>
492+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
493+
/// WinGet is not installed or not found on the system.
494+
/// </exception>
495+
/// <exception cref="System.ArgumentNullException">
496+
/// A provided argument is null.
497+
/// </exception>
498+
/// <exception cref="System.ArgumentException">
499+
/// A provided argument is empty.
500+
/// </exception>
501+
/// <exception cref="System.Security.SecurityException">
502+
/// The current user is missing administrator privileges for this call.
503+
/// </exception>
504+
public async Task<bool> DisableAdminSettingAsync(WinGetAdminOption setting)
505+
{
506+
ArgsHelper.ThrowIfObjectIsNull(setting, "setting");
507+
508+
return await DisableAdminSettingAsync(setting.EntryName);
509+
}
510+
//---------------------------------------------------------------------------------------------
511+
512+
//---Info--------------------------------------------------------------------------------------
262513
/// <summary>
263514
/// Gets all WinGet related data provided by the WinGet info action.
264515
/// </summary>
@@ -319,7 +570,9 @@ public async Task<WinGetInfo> GetInfoAsync()
319570

320571
return _outputReader.ToWingetInfo(result.Output, actionVersionId);
321572
}
573+
//---------------------------------------------------------------------------------------------
322574

575+
//---Protected Functions-----------------------------------------------------------------------
323576
/// <summary>
324577
/// Checks if the installed WinGet version is between the given versions or the same.
325578
/// </summary>
@@ -406,7 +659,9 @@ private protected async Task<ProcessResult> ExecuteAsync(string args, bool needs
406659

407660
return await _processManager.ExecuteWingetProcessAsync(args);
408661
}
662+
//---------------------------------------------------------------------------------------------
409663

664+
//---Other-------------------------------------------------------------------------------------
410665
/// <summary>
411666
/// Throws a <see cref="WGetNET.Exceptions.WinGetNotInstalledException"/> if winget installation could not be found.
412667
/// </summary>
@@ -571,5 +826,6 @@ private bool CheckAdministratorPrivileges()
571826

572827
return false;
573828
}
829+
//---------------------------------------------------------------------------------------------
574830
}
575831
}

0 commit comments

Comments
 (0)