Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Path of Windows Service

Status
Not open for further replies.

NeilV

Programmer
Oct 14, 2002
117
GB
Hello,

I am creating a windows service that needs to have the ability to read in some information from a text file that will be stored in the same directory as the service. How can I setup the path so it can find the file? In a normal vb.net application I use Application.startuppath but this is not available for a windows service...

Help!

Neil
 
Try ".\Myfile.dat"

The single dot refers to the current directory, which should be the same as where your service started.

Chip H.
 
Thanks Chip,

I tried that but it didnt work. My service still looks in the Windows\system32 directory for files - both to write to and to read from. I have tried including the full path to the file(s) but this has no effect.

Any ideas?

Neil
 
have you tried using request.applicationpath.tostring?
upworth
 
I forgot to mention that there are other *path* properties of request (i.e. request.physicalpath) that might be useful as well.

upworth
 
thanks for that but...
when i use request.xyz it gives me an error saying "Name request is not declared"

Neil
 
The name "request" is part of the system.web namespace. Try putting "imports system.web" at the top of your code or making sure that there is a reference pointing to system.web in your project. This "Request" name is also called "HttpRequest".

Hope this helps...
 
Ok, I've added that bit but I still get the same problem. I have also tried using Httprequest but this does not have any path type methods associated with it, this is the code i am trying:

Dim MyReq As System.Web.HttpRequest()
MyReq.ApplicationPath.ToString

The applicationpath bit doesn't seem to exist. I assume i'm doing something simple wrong!
 
Try the above without the "()" at the end. Then when you type in "MyReq." in the code, IntelliSense should show you the list of available methods, properties, etc.
 
getting closer...the intellisense bit now displays the correct methods - however, when i actually run my service i get the following error:

Object reference not set to an instance of an object.

Here is my code:

Dim MyReq As System.Web.HttpRequest

filename = MyReq.ApplicationPath.ToString()

Where filename is a string.

Thanks in advance
 
Try this...

******************************************
dim filename as string

' PhysicalPath includes the current file so I need to _
' remove the filename so I'm left with only the _
' directory structure. Ths IIF is there to make sure _
' no error is produced if it doesn't find a "/"
filename = Request.PhysicalPath.Substring(0, _
IIf(Request.PhysicalPath.LastIndexOf("\") > 0,_
Request.PhysicalPath.LastIndexOf("\"), 0))

' In my app, I needed to make sure that the path _
' included the "/" at the end. So, I added this line to _
' check for it - if it's not there, I add it.
If Not filename.EndsWith("/") Then filename &= "/"

' This results in a directory structure that your web _
' server understands. For example, the URL may be _
' but the physical path _
' is c:\inetpub\ In this case _
' filename = c:\inetpub\**************************************

In your code above in the previous post, when you created MyReq, you are creating a blank "Request" that you have to supply the info for. To get the path for the current web request, simply access web.request.??? Leave out the "Dim MyReq ..."

upworth
 
Thanks 4 that, I am creating a windowsservice though - should that make any difference?
 
I haven't tried it with a service, so I can't be too sure, but I would suspect that it's the same.

Basically, by creating a variable, MyReq, that is a web.Request type, then you are saying that you are going to manually populate all the properties. By dynamically accessing web.Request you get whatever the current environment warrants.

I would like to know how this performs for a web service, so I would appreciate you keeping us (me) informed. :)

I'm no expert, but I'm still plugging...
upworth
 
When you install a windows service it writes an entry to the registry. One entry is the path to the executable. Read that entry to get the path. It should be HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\servicename . Look for the value of ImagePath. Here is what I did

Private Function GetDir() As String
Try
Dim Reg As RegistryKey
Dim strResult As String
Dim s As Short
Reg = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\servicename", False)
strResult = Reg.GetValue("ImagePath", "")
Reg.Close()
s = InStrRev(strResult, "\", , CompareMethod.Text) - 1
Debug.WriteLine(strResult.Substring(0, s))
Return strResult.Substring(0, s)
Catch ex As Exception
WriteExcp(ex)
Return ""
End Try
End Function

Hope this helps.

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top