How do you handle "File Access Denied" when deleting a file with the FileInfo.Delete method in VB - vb.net

I am deleting log files >= 2 days old on a server with the following code:
'Iterate through all files in specified path
For Each file As IO.FileInfo In New IO.DirectoryInfo(sDirPath).GetFiles(sFileType)
'Delete all log files 2 days old or older
If (Now - file.CreationTime).Days >= iNumDays Then
file.Delete()
End If
Next
This code works find on a regular windows machine. On the server, it is setup to query the user for Administrative permissions and click continue to delete the file. I have admin rights on the server. My question is, how do you "click Continue" in code when the file.Delete method is executed?

Hope these are useful:
Create elevated Shortcut to run Program bypassing UAC Prompt in
Windows 8 |
7
Deactivate UAC in Windows 7 for a specific
program

Related

Excel Add-ins installed programmatically are not loaded

Question Summary:
Why do my add-ins which are installed programmatically do not load when I start Excel from Program Menu but are loaded when I open the Installer.xlsm workbook which installed them?
Details:
My team develops a hierarchy of add-ins used by various users in our organization.
I have an Installer.xlsm file which installs new versions for each of the add-ins. It has a Workbook_Open method which upon opening the workbook uninstalls currently installed versions of the add-ins and installs their newer versions.
This worked wonderfully for over a year. Recently we updated the hierarchy of the add-ins and since then the same script would run successfully, uninstalling old add-ins and installing new ones. However, when Excel is re-opened (from program menu or an existing workbook) it seems that the script had no effect at all - the add-ins that were installed before the run remain installed and the new add-ins are not even listed in the Manage Add-ins form. This is reflected also in HKCU\...\Excel\Add-in Manager - the list of add-ins remains identical to what it was before the run.
However, if I open Installer.xlsm again - it loads only the add-ins I'd expect it to load after a successful script run (loads the newly installed add-ins and doesn't load the ones the script uninstalled)! It's as if the add-ins are installed in the scope of the single .xlsm file...
Notes:
The add-ins are not placed in Excel's Add-in directory but in a project folder (C:\appname\Addins\date).
I use Excel 2010 on Windows 7 and have admin privilege on my machine.
It's not a matter of Registry write access, since the same add-ins, if installed using the Excel UI are registered correctly under HKCU\...\Excel\Add-in Manager.
Please help resolve this mystery!
Code section which does the uninstalling:
For Each ad In Application.AddIns
For Each appName In pAppNames
If pIniMap.item("FilePrefix").Exists(appName) Then
filePrefix = pIniMap.item("FilePrefix").item(appName)
If Left(ad.Name, Len(filePrefix)) = filePrefix Then
If ad.Installed Then
ad.Installed = False
Workbooks(ad.Name).Close False
End If
End If
Else
logger.Warn "Entry is missing for section FilePrefix, appName=" & appName, methodName
End If
Next appName
Next ad
Code section which does the installation of the add-ins:
For Each file In addinFiles
curAddInPath = pAddinDir & file
With Application.AddIns.Add(fileName:=curAddInPath)
.Installed = True
End With
Next file
It may be that your adding from the default directory rather than the custom directory you specified. I added the argument below, in your installation code. Hopefully it helps.
For Each file In addinFiles
curAddInPath = pAddinDir & file
With Application.AddIns.Add(fileName:=curAddInPath, CopyFile:=False) 'add false to args if addin is not coming from default directory
.Installed = True
End With
Next file

How do I perform file operations with administrator privileges?

I am trying to update some files in my application. I am trying to download the file, rename the current file to (.old) and then rename the downloaded file to the original file name (.exe).
When I run it in XP, it works fine. In Windows7, it gives me an error saying that access is denied. If I run the app from my desktop shortcut and set the properties of the shortcut to run as administrator, it works but it also prompts me to run the app as an admin every time I run it. I'm already logged in as an admin, so I don't want to be prompted every time I run it.
Is there a way in my program to simply rename the file as an administrator?
This is my rename code:
Dim old_file_name As String = Now.TimeOfDay.TotalMilliseconds & ".old"
File.Move(Application.StartupPath & "\myprog.exe", Application.StartupPath _
& "\" & old_file_name)
In My case I found that there is no good (quiet) way to perform an operation on Windows7 as an administrator without being prompted for elevated privileges. One option is to have the end user change their UAC settings so they don't get prompted and this is not a viable option. Another to is change my program manifest but that results in a prompt for admin privileges every time they run it, also not very desirable. Another is to simply change the shortcut properties to run as administrator, but again, that prompts for admin privileges every time you run it. So what I ended up with is simply letting my program run a the normal user and when they want to upgrade the program, they choose to perform the upgrade. The upgrade is handled by a separate program (which gets called from my main program) that does have its manifest changed to run as administrator, which will prompt the user for privileges. This way, they only get prompted when they want to run the upgrade program.

"Access to this path is denied" in Windows 7 application

Ran into a strange phenomena:
in Application A, the code:
Using sr = New StreamReader(path)
runs perfectly (where path is C:\tmp for example. The directory is on the local machine).
In Application B the same line, on the same directory throws the exception: "Access to the path is denied".
This happens when run in the IDE or by activating the exe file.
Some additional information:
Windows 7 Pro, 64bit VS 2013
The app.manifest of both application is identical. I've tried changing to all possible options of requestedExecutionLevel but the result was the same.
The target framework of the application is 4.
The exception is thrown whether I run as administrator or not.
I've set the permissions of the directory to full control for all accounts (including Administrator and Everyone).
I've deleted the directory and recreated it. Didn't help.
Ran Farbar scan which did not report any problems.
Application A is referencing the FILE C:\tmp. I expect this file isnt present on Application B's machine.
http://msdn.microsoft.com/en-us/library/vstudio/f2ke0fzy
Also ensure you are properly disposing of your StreamReader class after use. if you dont do this the file can remain locked.

How to disable UAC prompt using manifest file option in Visual Studio 2010

I want to execute autoit script using vb.net in vs 2010. While executing it keeps prompting. Is it possible to embed a manifest file in a class lib type project in vb.net. As currently I cant see any option to add a manifest. Or plz any other suggestions to disable that prompt to occur while executing a script from code.
There is no way to stop the prompt if you have UAC enabled and the script requires elevation to perform some task.
You can force the script to run elevated by doing something like the following:
Dim proc As New ProcessStartInfo
With proc
.FileName = "C:\Windows\regedit.exe" 'replace with your script name
If Environment.OSVersion.Version.Major >= 6 Then ' Windows Vista or higher
.Verb = "runas"
Else
' No need to prompt to run as admin
End If
Process.Start(proc)
End With
Your only other option is to turn UAC off

Microsoft Office Excel cannot access the file

I have create Office Excel application in my machine(XP). and i have placed the application in windows server 2008 giving the following error.
Microsoft Office Excel cannot access the file '\server\Input.xls'. There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.
both client and server systems installed office 2007 and added reference microsoftexcellibrary 12.0
created assembly for the application and calling the code from the form.
I was getting the exact same error, although in my case I was trying to run (from Tidal Enterprise Scheduler) a .NET app that used microsoft.interop.excel.
The answer is already in stackoverflow:
[Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx'.
I'll re-list the steps from that link:
Create directory “C:\Windows\SysWOW64\config\systemprofile\Desktop” (for 64 bit Windows) or “C:\Windows\System32\config\systemprofile\Desktop” (for 32 bit Windows)
Set full control permissions on Desktop directory above (for example in Win7 & IIS 7 & DefaultAppPool set permissions for user “IIS AppPool\DefaultAppPool”)
Of course, if you're running a scheduled task rather than WebApp, you'll need to make sure the service account you're using has permissions on that folder.
I had the same error when trying to save to:
C:\test.xlsx
When attempting to save to the root directory, it seemed to cause problems. Changing my path to:
C:\MyExcelTests\test.xlsx
Worked fine
I would make sure no Excel processes are running on the machine before you run this application, that should cover the last point.
The first point looks the most likely issue, the path looks like a partial path or an incorrect UNC path. Try putting the full path to the spreadsheet 'C:\server\input.xls' and see if that works.
To stop the middle point, in the workbook.open command you can specify to open as read-only, this will make sure the application can red the file even if it is locked open by another user.