473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.UnauthorizedAccessException] = {"Access to the path 'CR1100000043D20082009053331.PDF' 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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit
  3. Dim FSO, WshShell, Then_Date, WSHShellENV, TempFolder, Recycled    ' variables needed for main program(local scope)
  4. On Error Resume Next
  5. Set FSO = CreateObject("Scripting.FileSystemObject")            ' a new object to gain access to a computer's file system
  6. Set WshShell = WScript.CreateObject("WScript.Shell")            ' an object in type specified
  7. Set WSHShellENV = WSHShell.Environment("PROCESS")
  8.  
  9. Set drv = FSO.GetDrive("D")
  10. DoDir FSO.GetFolder("D:\TUSA\TUSA\CallCentre\CustomerReports")
  11. DoDir FSO.GetFolder("D:\TUSA\Tusa\CustomerPortal\CustomerReports")
  12.  
  13. Then_Date = DateAdd("d", -5, Date)   ' Files older than 5 days
  14. WScript.Echo "Cleaned Databases on " & Now & ". " & " Erased Files older than " & Then_Date
  15.  
  16. Sub DoDir(Folder)
  17.    Dim i, File, SubFolder, fstr, pos, last_mod        ' variables needed for function
  18.    Dim Now_Date, Then_Date                            ' variable used for today's date
  19.    Now_Date = Date                                    ' loads today's date into variable Now_Date
  20.    Then_Date = DateAdd("d",-5, Now_Date)            ' Files older than 5 days
  21.  
  22.  
  23.    Dim Findstr(0)                                    ' an array named Findstr with array element one
  24.    Findstr(0) = ".PDF"                                ' loads into array element 0, the .txt file extension
  25.    For Each File In Folder.Files                    ' For each file in folder.files
  26.      FStr = UCase(File.Path)
  27.  
  28.  
  29.      'WScript.Echo(FStr)
  30.      Pos = 0
  31.     for i = 0 to 0                    
  32.     if ((instr(FStr, Findstr(i)) > 0) And (File.DateCreated < Then_Date)) then        
  33.  
  34.             ' check whether there exists a string inside FStr 
  35.             ' which has an extension of any of the elements of FindStr
  36.  
  37.        'WScript.Echo(FStr)                            ' echos file string
  38.  
  39.        File.delete                                    ' if there is, simply delete it
  40.              Exit For
  41.           End if
  42.       Next
  43.    Next
  44.    For Each SubFolder in Folder.SubFolders
  45.       DoDir SubFolder
  46.    Next
  47. End Sub
  48.  
  49. Sub ClearFolder(Folder)
  50.    Dim File, SubFolder
  51.    For each file in Folder.Files
  52.        File.delete(True)
  53.    next
  54.    'For Each SubFolder in Folder.SubFolders
  55.     '  SubFolder.Delete(True)
  56.    'Next
  57. End Sub
  58.  
Thanks
Manoj
Mar 31 '10 #1
2 6156
tlhintoq
3,525 Expert 2GB
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...
Mar 31 '10 #2
tlhintoq
3,525 Expert 2GB
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?
Apr 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
6
by: Dan V. | last post by:
Is there a way to query a remote xml file periodically by not using web services? For Windows and Unix platforms. Is there a cheap software product that I can install on each client and my Windows...
6
by: Pat Carden | last post by:
Hi, We need to allow webusers to upload a file on our website (on Server3, all servers run Server 2003, remotely hosted) and eventually save it on our SBS Server (Server2) which is not exposed...
4
by: Daniel P. | last post by:
I'm using C#: Stream s = File.Open( @"\remotemachine\share\MyFile.txt" , FileMode.Open , FileAccess.Read , FileShare.Read ); I'm getting the following exception: "The referenced account...
3
by: Amie | last post by:
Hi, My .NET application has a script that checks whether an image file on a remote server exists using File.Exists. >From my localhost (Server A), I can access the image file via network path...
6
by: Marc Castrechini | last post by:
This is a classic double hop delegation issue, however its the first time we are setting this up so we are doing something incorrectly. If we run through the IDE or using a localhost path on the...
3
by: rn5a | last post by:
I am trying to delete records from a MS-Access DB table using the following query: strSQL=DELETE FROM MyTable WHERE =CInt(Request.QueryString("delete")) con.Execute strSQL When I run the app...
14
by: Peter | last post by:
..NET 3.5 I have a Windows Service application and it does remoting, but when a client incounters an error the client get the following error message "Server encountered an internal error....
5
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello! I'm trying to copy a file from another computer on the network that I do not have permission with my current logon details to access. If I open the folder using the Windows file manager...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.