Delete file in remote server using C#, Windows Service

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arulmanoj
    New Member
    • Mar 2009
    • 34

    #1

    Delete file in remote server using C#, Windows Service

    I have windows service which tries to delete a file in the IIS web server. The file path in the IIS server is "E:\WebApp" . I am able to get list of all the files in this path. But un able to delete the file. I am getting the below error "[System.Unauthor izedAccessExcep tion] = {"Access to the path 'CR1100000043D2 0082009053331.P DF' is denied."}"

    Both IIS server and Windows service machine have windows 2003 server OS. I am running the service as administrator user only.

    I need java script or VB Script to delete old files in a folder and it's subfolders in a local computer. I have to call this script in a batch file or by windows scheduler tasks.

    I have written the below VBScript to delete files and it is working fine. But I need the samething as batch(.bat) file with DOS commands.

    Code:
    Option Explicit
    Dim FSO, WshShell, Then_Date, WSHShellENV, TempFolder, Recycled	' variables needed for main program(local scope)
    On Error Resume Next
    Set FSO = CreateObject("Scripting.FileSystemObject")	        ' a new object to gain access to a computer's file system
    Set WshShell = WScript.CreateObject("WScript.Shell")	        ' an object in type specified
    Set WSHShellENV = WSHShell.Environment("PROCESS")
    
    Set drv = FSO.GetDrive("D")
    DoDir FSO.GetFolder("D:\TUSA\TUSA\CallCentre\CustomerReports")
    DoDir FSO.GetFolder("D:\TUSA\Tusa\CustomerPortal\CustomerReports")
    
    Then_Date = DateAdd("d", -5, Date)   ' Files older than 5 days
    WScript.Echo "Cleaned Databases on " & Now & ". " & " Erased Files older than " & Then_Date
    
    Sub DoDir(Folder)
       Dim i, File, SubFolder, fstr, pos, last_mod		' variables needed for function
       Dim Now_Date, Then_Date			                ' variable used for today's date
       Now_Date = Date					                ' loads today's date into variable Now_Date
       Then_Date = DateAdd("d",-5, Now_Date)		    ' Files older than 5 days
       
    
       Dim Findstr(0)					                ' an array named Findstr with array element one
       Findstr(0) = ".PDF"				                ' loads into array element 0, the .txt file extension
       For Each File In Folder.Files	                ' For each file in folder.files
         FStr = UCase(File.Path)
    	
    
         'WScript.Echo(FStr)
         Pos = 0
        for i = 0 to 0					
    	if ((instr(FStr, Findstr(i)) > 0) And (File.DateCreated < Then_Date)) then		
    
                ' check whether there exists a string inside FStr 
                ' which has an extension of any of the elements of FindStr
    	  
    	   'WScript.Echo(FStr)	                        ' echos file string
    	   
    	   File.delete					                ' if there is, simply delete it
          	   Exit For
          	End if
          Next
       Next
       For Each SubFolder in Folder.SubFolders
          DoDir SubFolder
       Next
    End Sub
    
    Sub ClearFolder(Folder)
       Dim File, SubFolder
       For each file in Folder.Files
       	File.delete(True)
       next
       'For Each SubFolder in Folder.SubFolders
        '  SubFolder.Delete(True)
       'Next
    End Sub
    Thanks
    Manoj
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    I have xxx, then do yyy then zzz happens. Here's my code ...
    Ok. You know what you have.
    What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. What is it you are looking for?
    I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.

    I'm not trying to bust your chops. Its just that while your question is obvious to you, it isn't to the rest of us. Are you asking how to get access... how to check permissions... how to change file attributes... how to use user impersonation.. . How to check if the file is locked because its in use elsewhere...

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      What have you tried/done/coded so far?
      What error(s)/exception(s)/problem(s) is your current code causing?
      Can you provide the relevant code segment that is causing the error?

      Comment

      Working...