File access problem with service

lynchbags

New member
Joined
Aug 19, 2005
Messages
2
Programming Experience
10+
Sorry for the long post, but I want to give enough information to explain the problem I'm running into. I'm writing a windows service in VB.Net that has to monitor specific directories and when a file is found in that directory it is moved to a processing directory. I use two methods of monitoring the directory. First is a manual scan function that handles all the files using a for loop that looks like this:
VB.NET:
For Each sFile in Directory.GetFiles(sPath)
	ProcessFile(sFile)
Next
I then use a similar loop and recursively call the manual scan function for each sub-directory. After that I set up a FileSystemWatcher to monitor the directory. I also have wrote code that tracks when the last notification for any file was received and jumps out if the previous notification for that file was received within the last few seconds so when multiple writes occur during the transfer of the file to the directory the application doesn’t try to process the same file multiple times. After that I have a loop that tries to open the file with write access to make sure that the file still isn’t in the middle of the file transfer before trying to move it to the processing directory. My code to do this looks like this:
VB.NET:
bFileOpened = False
For nCount = 1 To MAX_TRIES
	Try
		objFileStream = File.Open(sFilePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
		objFileStream.Close()
		bFileOpened = True

		Exit For
	Catch
		Thread.Sleep(WAIT_TIME)
	End Try
Next
This code has worked just fine for files stored on my local computer, even if I access it via a shared directory and UNC path. All I had to do was make sure the service was running with under the Local System account and make sure any directories that were used by the service were set up to give access to the Local System account. When I set up the application to monitor a remote server everything works fine up until this point. The service can see the file during the manual file scan, and gets notification through the FileSystemWatcher if I copy the file out to the directory while the service is running. For some reason that I can’t figure out the above code can’t gain write access to the file and the File.Open function throws the exception: "Access to the path "\\cmpfilep03\df...\testfile.txt" is denied." The directory and file both have the security privileges set up so the SYSTEM account has full access. I thought that the file might be in use by another process, but I can open the file in notepad, make a change, and save it without any problems. Does anybody have any ideas what could be causing this? Thanks.
 
1. ensure u have a known username & password on the remote server u r trying to access.

2. try making the service with the account as a USER and set the password property and the user property.
The user property should be mentioned like this "machinename\user"

try and let me know ...

chandru
 
I'd like to try to get the service running as local system because that is the preferred method for the server it is running on. The confusing thing is that with local system it can read the file with out any problems, but it can't write even though the system account has both read and write access for that directory.
 
Hi,

Be sure that the Folder you are trying to Write data has Writes Permission for the User who's account the Windows Services are running

Jay Kavimandan
 
Hi guys
Thanks for your concern and. I am looking for a code to send fax to a remote server in vb.net. I have a code which is working great when I send fax to a local server. I have used fxscom.dll. I have through the net, but it is saying that this dll cannot be used to send fax to remote server. Is it true .Its urgent. I shall be highly obliged if you provide me the code for sending fax in vb.net

thanks & regards
vinit...
 
I'm having a similar problem with using the file.openread on a file that is already in use, is there a way to read a file while its being used by another process? Any ideas?
 
Hi ,

i dont know if you guys can help, I have a windows service where it needs to copy a file form c:\tmp to \\Billingserver\d$\test . I created a standard windows application and executed the code to copy from c:\tmp to \\Billingserver\d$\test. It worked great, but when i run the windows service with the same code as the standard windows application , it accesses the c:\tmp directory but not the \\Billingserver\d$\test directory. It sais that the directory doesnt exist. (directory.exists("\\Billingserver\d$\test") ) returns false . Anyone know what the problem is?
 
more than likely colinwie you are having a permissions problem with the service. i posted my query in this thread: http://www.vbdotnetforums.com/showthread.php?p=20411#post20411 but will reiterate here in response to chandru and progskills advice.

even if one gives a service administrator rights, which should enable reading/writing across the network i still receive "acces denied" errors.

i am puzzled by this and the combinations of possible users for services. any time i try to install the service via the processInstaller as a network service it will not install correctly due to permissions. Admin and local system will install but will not copy my file across the network.

i have seen impersonation suggested but i do not know how to go about doing this (but i am still looking) any more suggestions?
 
Back
Top