Windows服务的默认路径 [英] Default path for Windows Service

查看:107
本文介绍了Windows服务的默认路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先将我的Windows服务作为常规Windows Exe编写,因为它更容易调试。在那里我使用了


AppDir = Application.ExecutablePath.Substring(0,

Application.ExecutablePath.LastIndexOf(" \"))''这个应用程序的位置

Excutable坐着


现在,当我将它转换为服务时,我可以使用哪条路径?或者我可以在
中指定服务中始终用作数据路径的路径吗?我更喜欢

前者,因为我不知道目标电脑的布局。


-

Anil Gupte
www.keeninc.net
www.icinema.com

I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.ExecutablePath.Substring(0,
Application.ExecutablePath.LastIndexOf("\")) '' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would prefer
the former because I do not know the layout of the target computer.

--
Anil Gupte
www.keeninc.net
www.icinema.com

推荐答案

9月13,11:39 am,Anil Gupte < anil -l ... @ icinema.comwrote:
On Sep 13, 11:39 am, "Anil Gupte" <anil-l...@icinema.comwrote:

我首先将我的Windows服务作为常规Windows Exe编写,因为它是

更容易调试。在那里我使用了


AppDir = Application.ExecutablePath.Substring(0,

Application.ExecutablePath.LastIndexOf(" \"))''这个应用程序的位置

Excutable坐着


现在,当我将它转换为服务时,我可以使用哪条路径?或者我可以在
中指定服务中始终用作数据路径的路径吗?我更喜欢

前者因为我不知道目标电脑的布局。


-

Anil Guptewww。 keeninc.netwww.icinema.com
I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.ExecutablePath.Substring(0,
Application.ExecutablePath.LastIndexOf("\")) '' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would prefer
the former because I do not know the layout of the target computer.

--
Anil Guptewww.keeninc.netwww.icinema.com



尝试使用WMI类获取Windows默认路径


Imports System.Management

公共类wmi

私有objOS作为ManagementObjectSearcher

私有objCS作为ManagementObjectSearcher

私有objMgmt作为ManagementObject

私有m_strComputerName为字符串

私有m_strManufacturer为字符串

私有m_StrModel为字符串

私有m_strOSName为字符串

私有m_strOSVersion为字符串

私有m_strSystemType为字符串

私有m_strTPM为字符串

私有m_strWindowsDir为字符串

公开Sub New()

objOS = New ManagementObjectSearcher(" SELECT * FROM

Win32_OperatingSystem")

objCS = New ManagementObjectSear cher(SELECT * FROM

Win32_ComputerSystem)

For each objMgmt in objOS.Get

m_strOSName = objMgmt(" name") .ToString()

m_strOSVersion = objMgmt(" version")。ToString()

m_strComputerName = objMgmt(" csname")。ToString()

m_strWindowsDir = objMgmt(" windowsdirectory")。ToString()

下一页


每个objMgmt在objCS.Get

m_strManufacturer = objMgmt(" manufacturer")。ToString()

m_StrModel = objMgmt(" model")。ToString()

m_strSystemType = objMgmt(" ; systemtype")。ToString

m_strTPM = objMgmt(" totalphysicalmemory")。ToString()

下一页

结束子


Public ReadOnly属性ComputerName()

获取

ComputerName = m_strComputerName

结束获取


结束物业

Public ReadOnly物业制造商()

获取

制造商= m_strManufacturer

结束获取


结束物业

Public ReadOnly物业模型()

获取

型号= m_StrModel

结束获取


结束物业

公开ReadOnly Property OsName()

获取

OsName = m_strOSName

结束获取


结束财产


Public ReadOnly Property OSVersion()

获取

OSVersion = m_strOSVersion

End Get


结束财产

公共ReadOnly属性SystemType()

获取

SystemType = m_strSystemType

结束获取


结束物业

Public ReadOnly物业TotalPhysicalMemory()

获取

TotalPhysicalMemory = m_strTPM

结束获取


结束物业


Public ReadOnly Property WindowsDirectory()

获取

WindowsDirectory = m_strWindowsDir

结束获取


结束财产

Public ReadOnly Property FontDirectory()

获取

FontDirectory = m_strWindowsDir& " \ font" .ToUpper

结束获取

结束财产

结束班

Try the WMI class to get Windows Default path

Imports System.Management
Public Class wmi
Private objOS As ManagementObjectSearcher
Private objCS As ManagementObjectSearcher
Private objMgmt As ManagementObject
Private m_strComputerName As String
Private m_strManufacturer As String
Private m_StrModel As String
Private m_strOSName As String
Private m_strOSVersion As String
Private m_strSystemType As String
Private m_strTPM As String
Private m_strWindowsDir As String
Public Sub New()

objOS = New ManagementObjectSearcher("SELECT * FROM
Win32_OperatingSystem")
objCS = New ManagementObjectSearcher("SELECT * FROM
Win32_ComputerSystem")
For Each objMgmt In objOS.Get
m_strOSName = objMgmt("name").ToString()
m_strOSVersion = objMgmt("version").ToString()
m_strComputerName = objMgmt("csname").ToString()
m_strWindowsDir = objMgmt("windowsdirectory").ToString()
Next

For Each objMgmt In objCS.Get
m_strManufacturer = objMgmt("manufacturer").ToString()
m_StrModel = objMgmt("model").ToString()
m_strSystemType = objMgmt("systemtype").ToString
m_strTPM = objMgmt("totalphysicalmemory").ToString()
Next
End Sub

Public ReadOnly Property ComputerName()
Get
ComputerName = m_strComputerName
End Get

End Property
Public ReadOnly Property Manufacturer()
Get
Manufacturer = m_strManufacturer
End Get

End Property
Public ReadOnly Property Model()
Get
Model = m_StrModel
End Get

End Property
Public ReadOnly Property OsName()
Get
OsName = m_strOSName
End Get

End Property

Public ReadOnly Property OSVersion()
Get
OSVersion = m_strOSVersion
End Get

End Property
Public ReadOnly Property SystemType()
Get
SystemType = m_strSystemType
End Get

End Property
Public ReadOnly Property TotalPhysicalMemory()
Get
TotalPhysicalMemory = m_strTPM
End Get

End Property

Public ReadOnly Property WindowsDirectory()
Get
WindowsDirectory = m_strWindowsDir
End Get

End Property
Public ReadOnly Property FontDirectory()
Get
FontDirectory = m_strWindowsDir & "\font".ToUpper
End Get
End Property
End Class


如果OP想要通往Windows

目录的路径,这可能会非常好。


但他没有'''吨。他想要

应用程序/服务可执行文件所在目录的路径。

" Omar Abid" < om *********** @ gmail.comwrote in message

news:11 ******************* ***@g4g2000hsf.googlegro ups.com ...
Well that might be all very well if the OP wanted the path to the Windows
directory.

But he doesn''t. He wants the path of the directory where the
application/service executable resides.
"Omar Abid" <om***********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...

9月13日上午11:39,Anil Gupte < anil -l ... @ icinema.comwrote:
On Sep 13, 11:39 am, "Anil Gupte" <anil-l...@icinema.comwrote:

>我首先将我的Windows服务作为常规Windows Exe编写,因为它更容易调试。在那我使用了

AppDir = Application.ExecutablePath.Substring(0,
Application.ExecutablePath.LastIndexOf(" \"))''这个应用程序在哪里
Excutable

现在,当我将其转换为服务时,我可以使用哪条路径?或者我可以指定服务中的路径始终用作数据路径吗?我会更喜欢
前者因为我不知道目标电脑的布局。

-
Anil Guptewww.keeninc.netwww.icinema.com
>I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.ExecutablePath.Substring(0,
Application.ExecutablePath.LastIndexOf("\")) '' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would
prefer
the former because I do not know the layout of the target computer.

--
Anil Guptewww.keeninc.netwww.icinema.com



尝试使用WMI类获取Windows默认路径


导入System.Management

公共类wmi

私有objOS作为ManagementObjectSearcher

私有objCS作为ManagementObjectSearcher

私有objMgmt作为ManagementObject

私有m_strComputerName作为字符串

私有m_strManufacturer为字符串

私有m_StrModel为字符串

私有m_strOSName为字符串

私有m_strOSVersion为字符串

私有m_strSystemType为字符串

私有m_strTPM为字符串

私有m_strWindowsDir为字符串


Public Sub New()


objOS =新的ManagementObjectSearcher(" SELECT * FROM

Win32_OperatingSystem")

objCS =新的ManagementObjectSearcher(" SELECT * FROM

Win32_ComputerSystem")

For each objMgmt in objOS.Get


m_strOSName = objMgmt(" name")。ToString()

m_strOSVersion = objMgmt(" version")。ToString()

m_strComputerName = objMgmt(" csname" ).ToString()

m_strWindowsDir = objMgmt(" windowsdirectory")。ToString()

下一页


For Each objMgmt在objCS.Get

m_strManufacturer = objMgmt(" manufacturer")。ToString()

m_StrModel = objMgmt(" model")。ToString()

m_strSystemType = objMgmt(" systemtype")。ToString

m_strTPM = objMgmt(" totalphysicalmemory")。ToString()

下一页

结束子


Public ReadOnly属性ComputerName()

获取

ComputerName = m_strComputerName

结束获取


结束物业

Public ReadOnly物业制造商()

获取

制造商= m_strManufacturer

结束获取


结束物业

Public ReadOnly物业模型()

获取

型号= m_StrModel

结束获取


结束物业

Public ReadOnly Property OsName()

获取

OsName = m_strOSName

结束获取


结束物业


Public ReadOnly Property OSVersion()

获取

OSVersion = m_strOSVersion

结束获取


结束物业

公共只读属性SystemType()

获取

SystemType = m_strSystemType

结束获取


结束物业

Public ReadOnly物业TotalPhysicalMemory()

获取

TotalPhysicalMemory = m_strTPM

结束获取


结束物业


Public ReadOnly Property WindowsDirectory()

获取

WindowsDirectory = m_strWindowsDir

结束获取


结束物业

Public ReadOnly Property FontDirectory()

获取

FontDirectory = m_strWindowsDir& " \ font" .ToUpper

结束获取

结束财产

结束班


Try the WMI class to get Windows Default path

Imports System.Management
Public Class wmi
Private objOS As ManagementObjectSearcher
Private objCS As ManagementObjectSearcher
Private objMgmt As ManagementObject
Private m_strComputerName As String
Private m_strManufacturer As String
Private m_StrModel As String
Private m_strOSName As String
Private m_strOSVersion As String
Private m_strSystemType As String
Private m_strTPM As String
Private m_strWindowsDir As String
Public Sub New()

objOS = New ManagementObjectSearcher("SELECT * FROM
Win32_OperatingSystem")
objCS = New ManagementObjectSearcher("SELECT * FROM
Win32_ComputerSystem")
For Each objMgmt In objOS.Get
m_strOSName = objMgmt("name").ToString()
m_strOSVersion = objMgmt("version").ToString()
m_strComputerName = objMgmt("csname").ToString()
m_strWindowsDir = objMgmt("windowsdirectory").ToString()
Next

For Each objMgmt In objCS.Get
m_strManufacturer = objMgmt("manufacturer").ToString()
m_StrModel = objMgmt("model").ToString()
m_strSystemType = objMgmt("systemtype").ToString
m_strTPM = objMgmt("totalphysicalmemory").ToString()
Next
End Sub

Public ReadOnly Property ComputerName()
Get
ComputerName = m_strComputerName
End Get

End Property
Public ReadOnly Property Manufacturer()
Get
Manufacturer = m_strManufacturer
End Get

End Property
Public ReadOnly Property Model()
Get
Model = m_StrModel
End Get

End Property
Public ReadOnly Property OsName()
Get
OsName = m_strOSName
End Get

End Property

Public ReadOnly Property OSVersion()
Get
OSVersion = m_strOSVersion
End Get

End Property
Public ReadOnly Property SystemType()
Get
SystemType = m_strSystemType
End Get

End Property
Public ReadOnly Property TotalPhysicalMemory()
Get
TotalPhysicalMemory = m_strTPM
End Get

End Property

Public ReadOnly Property WindowsDirectory()
Get
WindowsDirectory = m_strWindowsDir
End Get

End Property
Public ReadOnly Property FontDirectory()
Get
FontDirectory = m_strWindowsDir & "\font".ToUpper
End Get
End Property
End Class


Thanx,我会试试。


-

Anil Gupte
www.keeninc.net
www.icinema.com


" Stephany Young" < noone @ localhostwrote in message

news:el ************* @ TK2MSFTNGP06.phx.gbl ...
Thanx, I will try that.

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Stephany Young" <noone@localhostwrote in message
news:el*************@TK2MSFTNGP06.phx.gbl...

比使用SubString和LastIndexOf方法更简单的方法是:


AppDir = System.IO.Path.GetDirectoryName(Application.Execut ablePath)


如果您在服务中包含对System.Windows.Forms的引用,那么

您仍然可以使用它。您可以包含该参考而无需

包含表格。


另一种选择是:


系统。 Reflection.Assembly.GetExecutingAssembly()。位置


,有一些进口......可以写成:


AppDir =

路径。 GetDirectoryName([Assembly] .GetExecutingAssembly()。Location)


" Anil Gupte" < an ******* @ icinema.comwrote in message

news:eH ************** @ TK2MSFTNGP03.phx.gbl ...
An easier way than using the SubString and LastIndexOf methods is:

AppDir = System.IO.Path.GetDirectoryName(Application.Execut ablePath)

If you include a reference to System.Windows.Forms in your service then
you can still use that. You can include that reference without having to
include a form.

An alternative is:

System.Reflection.Assembly.GetExecutingAssembly(). Location

which, with some Imports ... can be written as:

AppDir =
Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)
"Anil Gupte" <an*******@icinema.comwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...

>>我首先将我的Windows服务作为常规Windows Exe编写,因为它更容易调试。在那我使用了

AppDir = Application.ExecutablePath.Substring(0,
Application.ExecutablePath.LastIndexOf(" \"))''这个应用程序在哪里
Excutable

现在,当我将其转换为服务时,我可以使用哪条路径?或者我可以指定服务中的路径始终用作数据路径吗?我会更喜欢前者,因为我不知道目标计算机的布局。

-
Anil Gupte
www.keeninc.net
www.icinema.com



这篇关于Windows服务的默认路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆