How to change Windows folder permission with VB.NET? (System.UnauthorizedAccessException) - windows

I am working on this small VB.NET application which needs to copy a couple files to a Windows folder but I am getting an access denied error; then if I go to the folder, right-click it and manually write the write permission to the folder everything will work fine.
However this application is going to be used by our employees in several machines of the network and mostly of them has not this level of "expertise" meaning that the process must happen smoothly and automatically and therefore the change of the permission should be done by the application itself.
I found a code snippet online which supposed to fix the problem but it is not working for me. I am getting a runtime error with the following message:
System.UnauthorizedAccessException: 'Attempted to perform an unauthorized operation.'
And this is the code I am using:
Dim folder As String = "C:\Program Files (x86)\MyCompany\MyApp"
Dim sid As SecurityIdentifier = New SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, Nothing)
Dim writerule As FileSystemAccessRule = New FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow)
Dim fsecurity As DirectorySecurity = Directory.GetAccessControl(folder)
fsecurity.AddAccessRule(writerule)
Directory.SetAccessControl(folder, fsecurity) 'the error happens here
The error happen exactly in the line where the permission setting try to apply. Any idea?

The solution is to level up the privileges of the application. Here it is how to force the program to do it automatically without the need of right-click it and choose "run as administrator" every time:
https://mobitek-system.com/blog/2020/06/how-to-run-vb-net-code-as-an-administrator/
😁

Related

Error installing GitHub desktop "Application cannot be started, contact the application vendor."

I've recently upgraded from windows 7 to windows 10 and when installing GitHub desktop, I'm getting the following error:
"Application cannot be started, contact the application vendor."
When I click on details, there is the following error:
"Unable to install this application because an application with the same identity is already installed."
I tried several troubleshoots, such as:
running the .exe file as administrator;
deleting the 2.0 folder from Local/apps/
disabling windows defender
But none of them seems to work. I cant understand what the problem may be, since I had no trouble installing github desktop on windows 7.
EDIT This method solved my problem:
Hold Windows button + R
Type in %TEMP%
Delete the entire content of temp.
I fixed this problem by adding permissions to the UserName for the of the folder containing GitHub.exe:
Using the explorer paste "%UserProfile%\AppData\Local\Apps\2.0
In the search box write "github.exe"
In the result list right-click the file & select open file location.
Right click the folder -> Properties->Security
Click [Edit...] for "Group or user names:"
Add the account user name to the list giving it full permission.
This may not apply to your situation, but here is what fixed the problem for me:
Find this file:
C:\Users[YOUR USER NAME HERE]\AppData\Local\GitHub\TheLog.txt
In that file, search for this string:
"System.Security.SecurityException: Requested registry access is not allowed."
If you find that, look down from that line, one at a time, until you find mention of some specific application. For me it was MS Visual Studio:
"at GitHub.IO.ExternalProgramLauncher.IsVisualStudioInstalled()"
Running as an administrator, run regedit and look for keys related to the application in question. If access to reading them is denied, rectify that by right-clicking on the name of the key and choosing 'permissions'. (I decline to provide instructions on how to use the permissions interface here, but it is fairly straightforward) If you can grant yourself permission, that may fix your problem. It worked for me.
If you want to find the exact correct key, download and use a tool from MicroSoft called 'Process Monitor'. Unfortunately, even when you filter to show only registry events, you will still have at least 25,000 or so to look through, and the list is not sortable. >_< This was not exactly easy, but by scrolling up from the bottom and looking for 'ACCESS DENIED' in the unsortable 'Result' column for a while I was able to find the exact registry event where access was denied which caused GH DT to fail. In my case it was this key:
HKEY_CLASSES_ROOT\VisualStudio.DTE.14.0
I ran regedit using my admin account, and ran it 'as administrator', found the key and changed the permissions. Oddly, it only worked when I added 'Administrators' (plural). When I added 'Administrator', it still refused to allow access to the key.
Hope this is related to your problem, or someone's who reads this. :)
Simply right click on the installation file and run as admin. It will automatically adjust itself to your system settings and install without error.

Issues while converting a windows app to windows service

I created a monitoring utility that checks cpu, ram, drive space stats and emails if the usage goes above set threshold. It works great in the system tray but I realized that the exe will stop when I log out of windows server. That led me to believe that I needed to create a windows service. I would like to use the existing GUI Form to save data to application settings and use those settings in windows service. Here are the steps I took so far,
Added a Windows Service class.
Modified the original code to get rid of any interactive items that were related to GUI Form.
Added the code to this class.
Added a Service installer.
Added this code to it-->
public ProjectInstaller()
{
InitializeComponent();
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Server Monitoring";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
Change Start up object to Utility.Program.
When I try installing this through installUtil I get this error
System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Use
rs\AdminUser\Desktop\Temp\Server' or one of its dependencies. The system cannot
find the file specified..
Thanks!
If you are saving these application settings into a file that is in the same directory as the Windows service, that is going to be your problem. All Windows Services are run in the C:/Windows directory (or a sub-directory in there) so when you access files you will need to do one of two things:
Change the executing directory
You can change the 'current directory' for the executing app back to the folder that contains the exe with the following line of code:
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
This will make all relative files become relative to the executable location once again.
Make all file request full paths
This one is easier for some files than others. System files are the hardest. So if you are trying to get to a .config file, that's going to be a nightmare.

Invalid TLV Record VB 6.0 CR9

I currently am getting the error:
Invalid TLV Record
When running the following block of code:
Private Sub PrinttoFile_Click()
Dim crApp As CRAXDRT.Application
Dim crRep As CRAXDRT.Report
Set crApp = New CRAXDRT.Application
Set crRep = crApp.OpenReport("C:\Documents and Settings\Administrator\Desktop\DansTest\Report3.rpt")
'...code to set report parameters, login information etc...
ExportReportToPDF crRep, "C:\Testing\ReportTest.pdf", "Beds Held"
End Sub
It errors on Set crRep = line. I've made sure that all my .dlls for CR9 are registered. The report was built using CR9 Reports and saved as such. CRAXDRT is the CR9 runtime activeX control. Any ideas?
Also, this is not a deployment issue. I am working on the same machine as the coding. This error is happening while debugging the original code, not while running an executable.
The answer to this problem turned out to be that my CRAXDRT.dll file for CR9 was in the Windows System32 folder. Even though the reference in VB 6.0 was pointing to the correct .dll, something about it was wrong.
I fixed the error by copying the .dll into the folder with the rest of the CR9 files, C:\Program Files\Common Files\CrystalDecisions\2.0\bin and redirected the reference here and it solve the error.
So in short, make sure your dll file is in the correct folder with the rest of the dll files.

Final installation folder debugging

When I install my finished product it runs fine until the very last action is done (a button that 'saves' the job).
The program pops this error up:
Unhandled exception has occurred in your application. If you click continue, the app will ignor this error and attempt to continue. If you click quit, the app will close.
Could not find a part of the path 'C:\Program Files (x86)\ATR\Save A Job\obj\Release\42\ATR\BackupProgram\Backup.xml
For some reason, the program is looking for the backup.xml file in the wrong place. (and seemingly added folders)
The way I'm calling the backup.xml file in the program is by:
(System.Environment.SpecialFolder.ProgramFile sX86 & "\ATR\Save A Job\Backup.xml")
Which, could be the wrong way, but it makes sense to me.
I would assume that the full path to the file location would be as follows:
C:\Program Files (x86)\ATR\Save A Job\backup.xml
However, it is looking for it in the wrong place (obvious from the error)
Anyone know why?
Got it fixed guys. It was the permissions to the installation folder. My user was set to "read/write only"...once i switched it to Full Control, everything worked fine

VBScript runtime error: ActiveX component can't create object: 'XStandard.MD5'

The script work on the local computer but not remotely. Any idea?
I'm getting VBScript runtime error "ActiveX component can't create object: XStandard.MD5".
strSource = "C:\WINNT"
Set objFSO = CreateObject(Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strSource)
Set colfiles = objFolder.Files
For each file in colfiles
Set objMD5 = CreateObject("XStandard.MD5")
Go to the DLL folder and use in command prompt > regsrv32 XMD35.dll
http://www.xstandard.com/en/documentation/xmd5/
When you want to instruct a remote computer to instantiate a given ActiveX object, you need to specify the name of the remote server when calling the CreateObject(progID, [serverName]) function.
Otherwise, the script engine attempts to instantiate the object from the local machine.
For example:
Set objMD5 = CreateObject("XStandard.MD5", nameOfYourServer)
And of course, the component must be available on the server.
More info about CreateObject at MSDN
I don't know if it has anything to do with the syntax of line 2. you are missing an open quotation. You have:
Set objFSO = CreateObject(Scripting.FileSystemObject")
You should have:
Set objFSO = CreateObject("Scripting.FileSystemObject")
I realize this is an old question, but google sent me here with the same problem. In my case it was a 32-bit vs 64-bit issue - I needed to use the 32-bit version of CScript to execute.
More details here http://blogs.msdn.com/b/helloworld/archive/2007/12/12/activex-component-can-t-create-object-when-creating-a-32-com-object-in-a-64-bit-machine.aspx
The XStandard.MD5 is a component that is NOT standard in all computers. it is something that needs to be installed. However even after installing you may still run into the same error. This may be because you are running the script in a 64bit system. for some reason the 64bit wscript won't use the xmd5.dll. however you can make a workaround by associating the scriptfiles to run on the 32bit version of wscript. by
going into your default programs. (Windows 8+ Right click the startbutton area [category view] Click Programs, then Default Programs then Associate a file type or protocol with a program) (Windows 7- click on start then click default programs)
Then click vbs then click change program, change it to
C:\Windows\SysWow64\Wscript.exe
NOTE ONLY ON 64BIT
As dmogle says in his comment, you need to make sure that the component is installed on the remote computer. Assuming that you've got access to that computer, download the dll to it and then run the command regsvr32 XMD5.dll as can be seen here.
On another note, your script looks a little funny, the twp last lines are:
For each file in colfiles
Set objMD5 = CreateObject("XStandard.MD5")
Which means that you'll be creating an instance of the MD5 object for each file. I've never used the component in question, but I'm fairly sure that you probably want to create only one instance of the object and then call the functions on that object multiple times. So I'd suggest changing the order of those lines to:
Set objMD5 = CreateObject("XStandard.MD5")
For each file in colfiles

Resources