amirhpr
asked on
System.IO.File.Move Error Access to the path is denied.
On my Windows service written in C#, there is a method that makes an app domain and then loads some assemblies for that app domain (all of these are inside a thread).
Im trying to add the following functionality:
1. Check to see if assemblies are loaded?
2. If yes then unload the app domain
3. Create a directory using System.IO.Directory.Create Directory
4. Back up DLL assemblies into the backup directory using System.IO.File.Move
5. Copy over the new DLL assemblies using the System.IO.File.Move
6. Reload the app domain
Only step 4 is not working and Im getting the Access to the path is denied. in the Application event where I log the exceptions.
I cannot rename, delete, or move the DLL assemblies.
I used the sysinternal procexp to make sure when I try to move the DLLs they are not using by anyone and still I cannot move them.
I wonder if anyone knows the right way of checking the securities and permissions so this can be done (I mean programmatically).
In other words, if you were I, how do you write your code?
Here is mine (please copy and paste into VS, the text format is changed here):
Assembly[] allAssemblies;
allAssemblies = appDomain.GetAssemblies();
// Check if a PARSER_*.dll assembly is loaded
foreach (Assembly assemblyFile in allAssemblies)
{
string assemblyName = assemblyFile.FullName;
if (assemblyName.Contains("PA RSER"))
isParserModuleLoaded = true;
}
// If parser assembly is already loaded then unload the app domain CopilotParserController
if (isParserModuleLoaded)
AppDomain.Unload(appDomain );
System.IO.DirectoryInfo sourceDir = new DirectoryInfo(Application. StartupPat h + "\\PhoneHomeServicesUpdate ");
System.IO.Directory.Create Directory( Applicatio n.StartupP ath + "\\PARSER Modules Backup");
System.IO.DirectoryInfo backupDir = new DirectoryInfo(Application. StartupPat h + "\\PARSER Modules Backup");
FileInfo[] destinFileList = dir.GetFiles("PARSER_*.*") ;
// Make backup
foreach (FileInfo destinFile in destinFileList)
try
{
CommonLoggingProvider.LogM essageDebu g("Make backup to {0}", backupDir.FullName);
System.IO.File.Move(destin File.FullN ame, backupDir.FullName + "\\" + destinFile.Name);
}
catch (Exception ex)
{
CPDataService.stats.Errors ++;
CommonLoggingProvider.LogE ventWarnin g(ex.Messa ge);
ErrorReporter.ReportExcept ion(ex, "Copilot Data Service <CPDataService.ExecutePars ers> exception");
}
Im trying to add the following functionality:
1. Check to see if assemblies are loaded?
2. If yes then unload the app domain
3. Create a directory using System.IO.Directory.Create
4. Back up DLL assemblies into the backup directory using System.IO.File.Move
5. Copy over the new DLL assemblies using the System.IO.File.Move
6. Reload the app domain
Only step 4 is not working and Im getting the Access to the path is denied. in the Application event where I log the exceptions.
I cannot rename, delete, or move the DLL assemblies.
I used the sysinternal procexp to make sure when I try to move the DLLs they are not using by anyone and still I cannot move them.
I wonder if anyone knows the right way of checking the securities and permissions so this can be done (I mean programmatically).
In other words, if you were I, how do you write your code?
Here is mine (please copy and paste into VS, the text format is changed here):
Assembly[] allAssemblies;
allAssemblies = appDomain.GetAssemblies();
// Check if a PARSER_*.dll assembly is loaded
foreach (Assembly assemblyFile in allAssemblies)
{
string assemblyName = assemblyFile.FullName;
if (assemblyName.Contains("PA
isParserModuleLoaded = true;
}
// If parser assembly is already loaded then unload the app domain CopilotParserController
if (isParserModuleLoaded)
AppDomain.Unload(appDomain
System.IO.DirectoryInfo sourceDir = new DirectoryInfo(Application.
System.IO.Directory.Create
System.IO.DirectoryInfo backupDir = new DirectoryInfo(Application.
FileInfo[] destinFileList = dir.GetFiles("PARSER_*.*")
// Make backup
foreach (FileInfo destinFile in destinFileList)
try
{
CommonLoggingProvider.LogM
System.IO.File.Move(destin
}
catch (Exception ex)
{
CPDataService.stats.Errors
CommonLoggingProvider.LogE
ErrorReporter.ReportExcept
}
ASKER
Thanks ShazbotOK,
To answer your questions:
1. If any application (including yours OR IIS) references the Assemblies and is in execution mode while you attempt a move it will fail.
No, I did check with sysinternal procexp in detail to make sure the DLL assemblies are not being loaded or used.
2. Also if they are read-only it will fail.
They are not read-only
3. Are you also sure that the destination directory is not the problem?
I can move into the destination directory with some other files.
4. The user account executing the process running the move has read/write/modify/delete access?
This cannot be the case since because the same account can read/write/modify/delete from to the same source and destination directories but not these DLLs.
Do you know how programmatically I can do some System.IO using admin credentials?
The reason Im asking because if I log in as myself to this Windows 2003 server (and Im in the Administrator) then I can rename the DLLs and copy the new ones even if the service is running and might be using the DLL.
Thnaks for your time and helps,
/'L'\ mir
To answer your questions:
1. If any application (including yours OR IIS) references the Assemblies and is in execution mode while you attempt a move it will fail.
No, I did check with sysinternal procexp in detail to make sure the DLL assemblies are not being loaded or used.
2. Also if they are read-only it will fail.
They are not read-only
3. Are you also sure that the destination directory is not the problem?
I can move into the destination directory with some other files.
4. The user account executing the process running the move has read/write/modify/delete access?
This cannot be the case since because the same account can read/write/modify/delete from to the same source and destination directories but not these DLLs.
Do you know how programmatically I can do some System.IO using admin credentials?
The reason Im asking because if I log in as myself to this Windows 2003 server (and Im in the Administrator) then I can rename the DLLs and copy the new ones even if the service is running and might be using the DLL.
Thnaks for your time and helps,
/'L'\ mir
ASKER
I found more useful info here that I'm going to give a try:
http://safari.oreilly.com/0735619301/ch13lev1sec15#snippet
http://safari.oreilly.com/0735619301/ch13lev1sec15#snippet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i hope this helps you solve the problem regarding your this one:
Do you know how programmatically I can do some System.IO using admin credentials?
The reason Im asking because if I log in as myself to this Windows 2003 server (and Im in the Administrator) then I can rename the DLLs and copy the new ones even if the service is running and might be using the DLL.
<system.web>
<identity impersonate="true" userName="domain\username" password="password"/>
</system.web>
under web.config
Do you know how programmatically I can do some System.IO using admin credentials?
The reason Im asking because if I log in as myself to this Windows 2003 server (and Im in the Administrator) then I can rename the DLLs and copy the new ones even if the service is running and might be using the DLL.
<system.web>
<identity impersonate="true" userName="domain\username"
</system.web>
under web.config
ASKER
HI Night-Eagle,
I don't know what you mean by start the process again.
I started this question but I found the answer myself and I put teh answer.
I was not sure if I can accept my answer because I startaed myself.
If I can please let me know and I make my solution as accepted solution.
Best regards,
amirhp
I don't know what you mean by start the process again.
I started this question but I found the answer myself and I put teh answer.
I was not sure if I can accept my answer because I startaed myself.
If I can please let me know and I make my solution as accepted solution.
Best regards,
amirhp
Are you also sure that the destination directory is not the problem? The user account executing the process running the move has read/write/modify/delete access?