Set windows clock in Asp.net c#
Asked Answered
K

2

6

I need to set windows clock in my Asp.net website (Written in C#)

I found a solution in stackoverflow at:

How do I set the Windows system clock to the correct local time using C#?

But when I use this I give exception:

A required privilege is not held by the client

My Application poll Identity is: LocalSystem

Kolnos answered 5/8, 2015 at 18:5 Comment(6)
Are you trying to set the Windows clock on the server or the client's computer?Schilt
It's server (2008 r2 Enterprise).Kolnos
@ImranShams Have you granted the user "Run as a Service" privelige via the Local Security Policy? For some more info please refer this and thisCircumcise
let me check @WebrusterKolnos
@Webruster I can't set privelige, My Server OS is: 2008R2. Can u tell me how set it please?Kolnos
@ImranShams Windows 7: Date and Time - Allow or Prevent Users and Groups from Changing. Server 2008 R2 is the server equivalent of Windows 7.Woven
T
0

it is not a matter of "I need exact C# function for my problem", this is misguided. This is a security issue. Meddling with the clock is bad. Anyhow, to solve this you may try running the application pool under a user account that is member of the Administrators group and disable UAC.

You wont be able to change the clock on remote clients.

... also don't run an antivirus!

Oh, and disable clock synchronization of your system settings.

Turnspit answered 13/9, 2016 at 20:13 Comment(1)
To the down-voter: He is trying to set the computer's clock from code on his web app, and not just to display the time in a page.Turnspit
H
-1
<

div class="dp-highlighter"><div class="bar"><div class="tools">view plaincopy to clipboardprint?</div><div class="columns"><div>·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150</div></div></div>

    protected void Page_Load(object sender, EventArgs e)  
    {  
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");  

        }  
        protected void TimerTime_Tick(object sender, EventArgs e)  
        {  
            lblTime.Text = DateTime.Now.ToString("hh:mm:ss");  
        }  

    <asp:ScriptManager ID="ScriptManager1" runat="server">  
            </asp:ScriptManager>  
            <asp:Timer ID="TimerTime" runat="server" Interval="1000">  
            </asp:Timer>  
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
            <ContentTemplate>  
            <asp:Label ID="lblDateToday" runat="server"></asp:Label>  
                    <asp:Label ID="lblTime" runat="server"></asp:Label>  
                </ContentTemplate>  
                <Triggers>  
                    <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />  
                </Triggers>  
            </asp:UpdatePanel>  
            </div>  
            <br />  

</div>

protected void Page_Load(object sender, EventArgs e)
{
        lblTime.Text = DateTime.Now.ToString("hh:mm:ss");


    }
    protected void TimerTime_Tick(object sender, EventArgs e)
    {
        lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
    }


<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Timer ID="TimerTime" runat="server" Interval="1000">
        </asp:Timer>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:Label ID="lblDateToday" runat="server"></asp:Label>
                <asp:Label ID="lblTime" runat="server"></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="TimerTime" EventName="Tick" />
            </Triggers>
        </asp:UpdatePanel>
        </div>
        <br />

check out http://forums.asp.net/t/1528616.aspx?How+to+diplay+real+time+timing+clock+on+asp+net+web+page https://msdn.microsoft.com/en-us/library/ms172517(v=vs.90).aspx

Henkel answered 14/9, 2016 at 13:16 Comment(1)
He is trying to set the computer's clock, not to display the time in a page.Turnspit

© 2022 - 2024 — McMap. All rights reserved.