Last active
August 29, 2015 14:05
-
-
Save bvli/fd9ded5e7ccb3c3bb534 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2014 Bjarke Lindberg, Beverli.NET | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
// This service removes any file with the .url extension from the common desktop directory | |
// (usually c:\users\public\desktop) as soon as it is created and set the browser homepage | |
// to about:blank | |
// | |
// Some system adminstrators think that it is funny to push out url shortcuts to all users | |
// desktop using a group policy. Running this service will delete it before it shows up on | |
// your desktop. | |
// | |
// How to install: | |
// - Save this file to disk (E.g. as RemoveServicePortalIconService.cs). | |
// - Run %windir%\Microsoft.NET\Framework\v4.0.30319\csc.exe RemoveServicePortalIconService.cs. | |
// This will create a RemoveServicePortalIconService.exe file. | |
// - Create directory %programfiles%\RemoveServicePortalIconService (md %programfiles%\REmoveServicePortalIconService). | |
// - Copy the RemoveServicePortalIconService.exe to the newly created directory (copy RemoveServicePortalIconService.exe %programfiles%\RemoveServicePortalIconService) | |
// - change to the newly created directoy (cd %programfiles%\REmoveServicePortalIconService) | |
// - Install the service by running %windir%\Microsoft.NET\Framework\v4.0.30319\IntallUtill.exe RemoveServicePortalIconService.exe) | |
// - Start the service (net start "Remove Service Portal Icon"). | |
// - Enjoy your clean desktop. | |
using System; | |
using System.ComponentModel; | |
using System.Configuration.Install; | |
using System.Diagnostics; | |
using System.IO; | |
using System.ServiceProcess; | |
using Microsoft.Win32; | |
namespace B.Remove.ServicePortalIcon.Service | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
var service = new Service(); | |
var ServicesToRun = new[] { service, }; | |
ServiceBase.Run(ServicesToRun); | |
} | |
} | |
[DesignerCategory("Default")] | |
sealed class Service : ServiceBase | |
{ | |
private readonly FileSystemWatcher watcher; | |
private readonly string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory); | |
private readonly string filter = "Service Portal.url"; | |
public Service() | |
{ | |
this.watcher = new FileSystemWatcher(this.path, "*.url"); | |
this.CanStop = true; | |
this.CanPauseAndContinue = true; | |
this.AutoLog = true; | |
this.ServiceName = "RemoveServicePortalIconService"; | |
} | |
protected override void OnStart(string[] args) | |
{ | |
this.watcher.Created += this.OnWatcherCreated; | |
this.watcher.Changed += this.OnWatcherCreated; | |
this.watcher.Renamed += this.OnWatcherCreated; | |
this.watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName; | |
this.OnStartOrResume(); | |
} | |
protected override void OnStop() | |
{ | |
this.OnStopOrPause(); | |
this.watcher.Created -= this.OnWatcherCreated; | |
this.watcher.Changed -= this.OnWatcherCreated; | |
this.watcher.Renamed -= this.OnWatcherCreated; | |
} | |
protected override void OnPause() | |
{ | |
base.OnPause(); | |
this.OnStopOrPause(); | |
} | |
protected override void OnContinue() | |
{ | |
base.OnContinue(); | |
this.OnStartOrResume(); | |
} | |
private void OnStopOrPause() | |
{ | |
this.watcher.EnableRaisingEvents = false; | |
} | |
private void OnStartOrResume() | |
{ | |
this.watcher.EnableRaisingEvents = true; | |
this.CleanUpTheMess(Path.Combine(this.path, this.filter)); | |
} | |
private void OnWatcherCreated(object sender, FileSystemEventArgs e) | |
{ | |
this.CleanUpTheMess(e.FullPath); | |
} | |
internal void CleanUpTheMess(string fileName) | |
{ | |
if (File.Exists(fileName)) | |
{ | |
try | |
{ | |
File.Delete(fileName); | |
} | |
catch (IOException ex) | |
{ | |
Trace.WriteLine(string.Format("Could not delete file. File: '{0}' Error: '{1}'.", fileName, ex)); | |
} | |
catch (UnauthorizedAccessException ex) | |
{ | |
Trace.WriteLine(string.Format("Could not delete file. File: '{0}' Error: '{1}'.", fileName, ex)); | |
} | |
} | |
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Policies\Microsoft\Internet Explorer\Main", writable: true)) | |
{ | |
if (key != null) | |
{ | |
try | |
{ | |
key.SetValue("Start Page", "about:blank"); | |
} | |
catch (UnauthorizedAccessException) | |
{ | |
Trace.WriteLine("Could not write new home page to registry."); | |
} | |
} | |
} | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
base.Dispose(disposing); | |
using (this.watcher) { } | |
} | |
} | |
[RunInstaller(true)] | |
[DesignerCategory("Code")] | |
public sealed class ProjectInstaller : Installer | |
{ | |
public ProjectInstaller() | |
{ | |
var serviceProcessInstaller = new ServiceProcessInstaller | |
{ | |
Account = ServiceAccount.LocalSystem, | |
}; | |
var serviceInstaller = new ServiceInstaller | |
{ | |
DisplayName = "Remove Service Portal Icon", | |
ServiceName = "RemoveServicePortalIconService", | |
StartType = ServiceStartMode.Automatic, | |
}; | |
this.Installers.AddRange(new Installer[] { serviceProcessInstaller, serviceInstaller }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now reset the homepage to about:blank, in case an administrator thinks it's funny to have the power to decide your homepage!