Eradicator while your advice is good to consider, you should realize that some people do actually have the need to modify parts of the system, in fact I had to do exactly this task that the OP is asking about, it was required as a part of my job. Although in my case my application is the whole system (Kiosk), so maybe I'm the exception to the rule, but I'm sure there is a valid reason for why this is needed.
You can't change the screensaver time without Admin rights however.
Here is the code I used
if (SystemParametersInfo(SETTIMEOUT,time,0,0) == 0)
{
int eCode = Marshal.GetLastWin32Error();
if (eCode != 0)
{
throw new SSException(
util.getErrorMessage(eCode));
}
}
the util.getErrorMessage looks like this
/// <summary>
/// Take in a Win32 Error Code from Marshal.GetLastWin32Error() and
/// return the relevant string.
/// </summary>
/// <param name="eCode">The error code to get a string for.</param>
/// <returns>Error message or null if failure to retrieve string.</returns>
public static string getErrorMessage(int eCode)
{
IntPtr lpMsgBuf= IntPtr.Zero;
uint dwChars= FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
IntPtr.Zero,
(uint)eCode,
0, // Default language
ref lpMsgBuf,
0,
IntPtr.Zero);
if (dwChars==0)
{
// Handle the error.
int le= Marshal.GetLastWin32Error();
return null;
}
string sRet= Marshal.PtrToStringAnsi(lpMsgBuf);
// Free the buffer.
lpMsgBuf= IntPtr.Zero;
return sRet;
}
I'll let you figure out the P/Invoke yourself it's not hard.