|
|
I already know what time the computer/system started up.
I want to know what time the "System" process, as viewed in the Task Manager started.
|
|
|
|
|
I already know what time the computer/system started up.
I want to know what time the "System" process, as viewed in the Task Manager started.
|
|
|
|
|
For what purpose?
Task Manager says mine started in 2009 which is certainly untrue. So I don't see how you can do better.
P.S. Even as the administrator on my home system (Win 7) I still get the access violation when I try to get the StartTime of the System process.
modified 20-Mar-12 9:49am.
|
|
|
|
|
You need to run it as System (mine impersonates System to get the info, Administrator doesn't have enough privileges)
The purpose is a) because it completes the Process listing and allows me to cross-reference against netstat & system boot-time.
|
|
|
|
|
CCodeNewbie wrote: because it completes the Process listing
Then just display what the StartTime property tells you and leave it at that.
|
|
|
|
|
The StartTime property, once manipulated to reveal a value, tells you 1601/1/1 00:00:00.
That wasn't good enough for me. I wanted the actual StartTime but didn't have the syntactical knowledge of how to get it even if I did have rough idea of how it could be retrieved.
Thanks to you, Alan, Ravi & Luc, I (and anybody else Googling "get Window's System process starttime" or something similar), instead of hitting dozens of sites saying it can't be done, now have a solution.
I would like to think that's what boards like this are for.
Thank you once again for your input.
|
|
|
|
|
Still not perfect - but it works (Improvements very welcome)
TimeSpan uptime = TimeSpan.Zero;
using (PerformanceCounter ut = new PerformanceCounter("System", "System Up Time"))
{
ut.NextValue();
uptime = TimeSpan.FromSeconds(ut.NextValue());
}
Console.WriteLine(uptime.ToString());
Process[] processlist = Process.GetProcesses();
foreach (Process procs in processlist)
{
if (procs.ProcessName == "System")
{
long st = procs.StartTime.ToFileTime();
DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
float Days = (Environment.TickCount / 86400000);
int dd = (int)Days;
float Hours = (Environment.TickCount / 3600000 % 24);
int HH = (int)Hours;
float Mins = (Environment.TickCount / 60000 % 60);
int mm = (int)Mins;
float Secs = (Environment.TickCount / 1000 % 60);
int ss = (int)Secs;
DateTime result = DateTime.Now - new TimeSpan(dd, HH, mm, ss);
}
}
code run @ ~ 12:24 pm
|
|
|
|
|
I'm glad you've come to a solution that you are happy with. One point that I'm not sure you appreciate is that the up time obtained from the performance counter and the TickCount are essentially the same value.
private void CompareCounterAndTickCount() {
using (PerformanceCounter ut = new PerformanceCounter("System", "System Up Time")) {
ut.NextValue();
TimeSpan counterUpTime = TimeSpan.FromSeconds(ut.NextValue());
TimeSpan tickCountUpTime = TimeSpan.FromMilliseconds(Environment.TickCount);
Console.WriteLine("Up Time");
Console.WriteLine(" from performance counter {0}", counterUpTime);
Console.WriteLine(" from tickcount {0}", tickCountUpTime);
}
}
On my system the duration reported by TickCount is ~0.2s longer than the performance counter's value which presumably just means the two counters do not share the same origin. Also note the use of the FromMilliseconds method to convert the TickCount to a TimeSpan, much simpler than your multistep calculation!
Alan.
|
|
|
|
|
Yes, but I am not trying to get the system start-time. I can already get that using Ravi's post (see my previous question). I explicitly want the System process's start-time.
|
|
|
|
|
I can't understand your reasoning in this matter.
The SystemUp Time performance counter description is
System Up Time is the elapsed time (in seconds) that the computer has been running since it was last started. This counter displays the difference between the start time and the current time.
The Environment.TickCount description is
A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.
Subtracting either from the current time gets the time when the computer was started. I don't see why (or how) you differentiate the system start time and the "System" process start time.
Alan.
|
|
|
|
|
Hi Alan,
The reason(s) for wanting the System process start-time never had anything to do with getting the PCs boot-time.
It had to do with being able to complete a running-processes table, x-reference against 2 other sql tables that form part of the project and possibly because I saw so many forum postings that said it can't be done
Thanks for all your help with this & when I have completed phase 2 and/or 3 I will probably post the whole thing as an article and look forward to your comments.
|
|
|
|
|
|
|
I don't know what to do about it yet. The pseudo -code posted is a prototype to demonstrate that getting the "System" process's StartTime can be exposed (accurately when coded correctly).
What would your suggestion be?
|
|
|
|
|
|
Hi Luc,
Got it working but wanted to say thank you for your help (code is in my reply to PIEBALDconsult).
Would welcome your thoughts and comments...
|
|
|
|
|
Your calculation is wrong.
Quote: stime += (Environment.TickCount / 120000 % 60);
Can you see it now?
Alan.
|
|
|
|
|
Yup, saw it - thanks for pointing it out.
Got it working, code is in my reply to PIEBALDconsult.
Wanted to say a quick thanks for your help.
modified 20-Mar-12 8:33am.
|
|
|
|
|
Good Morning,
I am trying to extract a column of data from an Excel spreadsheet and pass it to a list box. I thought this was straight forward and I have found several very similar code samples through Google and Code Project search, but I cannot get any to work. I have created several variations of each with my own syntax (which is usually simpler) but I keep getting this error:
ERROR: "The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data."
I have tried adding a piece of code that I found to make sure the process is not running (just guessing here...see Sample 1), but I do not think that it works either. Below are 2 different code snippets that produce the same error. I would appreciate it these can be corrected of if a new code sample can be demonstrated to cure this issue ot produce the desired result.
Sample 1
private DataTable GetExcel(string fileName, string sheetName)
{
DataTable dt = new DataTable(sheetName);
try
{
string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", fileName);
string Sql = "SELECT * FROM [" + sheetName + "$]";
OleDbConnection Conn = new OleDbConnection(ConStr);
OleDbCommand Comm = new OleDbCommand(Sql, Conn);
OleDbDataAdapter da = new OleDbDataAdapter(Comm);
Process[] process;
process = System.Diagnostics.Process.GetProcessesByName("Excel");
if (process.Length > 0)
{
process[0].Kill();
}
Conn.Open();
da.Fill(dt);
Conn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return dt;
}
Sample 2
private void GetExcelInfo()
{
string theFile = textBoxExcelFile.Text;
string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + theFile + ";Extended Properties=Excel 8.0";
string sheetName = "Sheet1";
OleDbConnection Conn = new OleDbConnection(ConStr);
try
{
Conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "$]", Conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
Conn.Close();
}
}
Thank you of your help...Pat
|
|
|
|
|
Do you have the file open in Excel?
|
|
|
|
|
No. The app uses the openFileDialog to create the path and put it in a textbox so I can transfer it to the connection string. It looks like so:
C:\Users\Pat\Documents\Visual Studio 2010\Projects\Perfect 2012\Perfect 2012\bin\Debug\Filename.xls
Also, I ran the following code to determine all the running processes at the time of the call:
ArrayList allProcesses = new ArrayList();
foreach(Process p in processlist)
{
allProcesses.Add(p);
}
No Excel processes were revealed. I am baffled. Thanks for your question...Pat
|
|
|
|
|
How about the SPACEs in the path?
|
|
|
|
|
Is you're project being targeting "AnyCPU" and is your version of Windows 64-bit?? If so, then you can't use the Jet 4.0 provider. It's a 32-bit only piece of code and you can't mix 32- and 6-4bit code in the same process.
Try setting your compile options to target x86 only.
|
|
|
|
|
Thank you Dave,
This is a 32 bit system and I am already configured for x86. I appreciate your reply. Let me know if anything else comes to mind or if you see a problem in the code...Best, Pat
|
|
|
|