Process.MainModule.Filename is in DOS format

G

Guest

I am writing a C# app in which I need to enumerate the processes running on
the PC. I have succesfully done this as follows (assuming the app is running
on NT4, XP, 2000 or 2003):

ManagementScope ms = new
System.Management.ManagementScope("\\\\localhost\\root\\cimv2");
ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM
Win32_Process");
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
foreach ( ManagementObject mo in queryCollection)
{

but I now need the path of the process executable. ManagementObject has a
property "ExecutablePath" but it always seems to be blank, so I use:

PropertyData processIdProperty = mo.Properties["ProcessId"];
Process process =
Process.GetProcessById(Int32.Parse(processIdProperty.Value.ToString()));

but now I discover to my horror that, even under XP, the value of
process.MainModule.FileName is a DOS-format short filename with tildes, which
is not what I want.

Can anyone tell me how to get the executable path of the process in
long-filename format?
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

You can call the GetLongPathName API function through the P/Invoke layer
to get the long filename given it's short name.

Hope this helps.
 
G

Guest

Thanks Nicholas, that's just what I needed. It all seemed so much easier
before Manged Code!

Do you know of a resource anywhere that can be used as a "reverse lookup"
for API calls - ie. "is there an API call that does this?"?
--
Dave


Nicholas Paldino said:
Dave,

You can call the GetLongPathName API function through the P/Invoke layer
to get the long filename given it's short name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave said:
I am writing a C# app in which I need to enumerate the processes running on
the PC. I have succesfully done this as follows (assuming the app is
running
on NT4, XP, 2000 or 2003):

ManagementScope ms = new
System.Management.ManagementScope("\\\\localhost\\root\\cimv2");
ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM
Win32_Process");
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
foreach ( ManagementObject mo in queryCollection)
{

but I now need the path of the process executable. ManagementObject has a
property "ExecutablePath" but it always seems to be blank, so I use:

PropertyData processIdProperty = mo.Properties["ProcessId"];
Process process =
Process.GetProcessById(Int32.Parse(processIdProperty.Value.ToString()));

but now I discover to my horror that, even under XP, the value of
process.MainModule.FileName is a DOS-format short filename with tildes,
which
is not what I want.

Can anyone tell me how to get the executable path of the process in
long-filename format?
 
C

Colin Neller

I use
http://msdn.microsoft.com/netframew...ull=/library/en-us/dndotnet/html/win32map.asp
for my "reverse lookups"

--
Colin Neller
http://www.colinneller.com/blog


Dave said:
Thanks Nicholas, that's just what I needed. It all seemed so much easier
before Manged Code!

Do you know of a resource anywhere that can be used as a "reverse lookup"
for API calls - ie. "is there an API call that does this?"?
--
Dave


Nicholas Paldino said:
Dave,

You can call the GetLongPathName API function through the P/Invoke
layer
to get the long filename given it's short name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave said:
I am writing a C# app in which I need to enumerate the processes running
on
the PC. I have succesfully done this as follows (assuming the app is
running
on NT4, XP, 2000 or 2003):

ManagementScope ms = new
System.Management.ManagementScope("\\\\localhost\\root\\cimv2");
ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM
Win32_Process");
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
foreach ( ManagementObject mo in queryCollection)
{

but I now need the path of the process executable. ManagementObject has
a
property "ExecutablePath" but it always seems to be blank, so I use:

PropertyData processIdProperty = mo.Properties["ProcessId"];
Process process =
Process.GetProcessById(Int32.Parse(processIdProperty.Value.ToString()));

but now I discover to my horror that, even under XP, the value of
process.MainModule.FileName is a DOS-format short filename with tildes,
which
is not what I want.

Can anyone tell me how to get the executable path of the process in
long-filename format?
 
G

Guest

That looks excellent. Many thanks.
--
Dave


Colin Neller said:
I use
http://msdn.microsoft.com/netframew...ull=/library/en-us/dndotnet/html/win32map.asp
for my "reverse lookups"

--
Colin Neller
http://www.colinneller.com/blog


Dave said:
Thanks Nicholas, that's just what I needed. It all seemed so much easier
before Manged Code!

Do you know of a resource anywhere that can be used as a "reverse lookup"
for API calls - ie. "is there an API call that does this?"?
--
Dave


Nicholas Paldino said:
Dave,

You can call the GetLongPathName API function through the P/Invoke
layer
to get the long filename given it's short name.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am writing a C# app in which I need to enumerate the processes running
on
the PC. I have succesfully done this as follows (assuming the app is
running
on NT4, XP, 2000 or 2003):

ManagementScope ms = new
System.Management.ManagementScope("\\\\localhost\\root\\cimv2");
ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM
Win32_Process");
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
foreach ( ManagementObject mo in queryCollection)
{

but I now need the path of the process executable. ManagementObject has
a
property "ExecutablePath" but it always seems to be blank, so I use:

PropertyData processIdProperty = mo.Properties["ProcessId"];
Process process =
Process.GetProcessById(Int32.Parse(processIdProperty.Value.ToString()));

but now I discover to my horror that, even under XP, the value of
process.MainModule.FileName is a DOS-format short filename with tildes,
which
is not what I want.

Can anyone tell me how to get the executable path of the process in
long-filename format?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top