Unable to delete .exe file through c# - c#

I have an update button within the windows form application.When the user clicks on the update button, the application checks the current version of the application with the version available in the server obtained from the webservice. If there is mismatch among versions,the application will download the new version from the path obtained from the webservice.
I am currently using two projects within the same solution
Main project where the application is running
Update project -Its purpose is used to delete the .exe file and download the new .exe file. (Update project is added as a reference of Main project)
The problem is when i try to delete the mainproject.exe through code in update project,it shows an exception saying "Unauthorized exception caught". Does anyone knows why this is happening?OR Does anyone have a better idea to use update function within the application??
This is the code that i am using for the deleting the file.
Unauthorized Exception in Windows Forms - C#
Edit:-
While i was debugging the application,iam able to delete the .exe file.But when i try to delete the application after installing in the desktop,again iam getting the exception message as "Access is denied".

In you update button, you start another small app as a separate process , in the small app, you can use the following code to kill your process, and then delete the original app.
try
{
Process [] proc Process.GetProcessesByName("YourAppName");
proc[0].Kill();
}

I found the solution why iam getting the "access is denied" exception in my application.
Since iam deleting a file inside the application through code i need to have the privilege of "Administrator".
One way is to make the user login manually as administrator.But that is not a better option.
Another way is to create an App Manifest file within your project and set the level as "administartor."
Creating App Manifest--> Right click on the project->Add new item-->Select App Manifest option from the right pane->Click ok
Open the manifest file and change the level to "requireAdministartor".
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
This will solve the issue while running the application,it will prompt user to run as administrator.
Hope this will be helpful to someone in future. :) Thank you guys for your support.

Is the application running and that's why it is unable to delete the executable? If so, you could rename the running executable and put the new version in its place. The new version will then be executed the next time the application is started.

Related

App.Config on Winforms is getting reinstalled

I have a windows application installed and working perfectly fine.
Something weird came up, the test scenario is if I rename the config file from the original name which is:
Model Label PC Client.exe.config
...to:
xxxModel Label PC Client.exe.config
...and click the .EXE file on the application folder / path where i installed it, it works as expected.
But when I clicked the shortcut icon of this application on the desktop, it shows a pop up that installs a new copy of the correct config file name.
please refer to the screenshot , any idea how to prevent this from happening? or is this really the behavior?
OP:
"any idea how to prevent this from happening?"
For installed apps, it is by design. Essentially you have removed an installed file and so the installer tech will kick in to recover it as if nothing happened.
Now technically you could get around that auto-recovery by not using MSI tech to install your app trusting instead on good-ol' XCOPY but then again, renaming/moving/deleting the file may break the app because this time there is no auto-recovery!
Additionally .NET apps expect to find a .config file matching the same name as the executable so you shouldn't go renaming/deleting/moving it in most scenarios.

error CS2012: Cannot open <executable path> access to <executable path denied>

I was working on a WPF application in Visual Studio 2015 and all went well until unexpectedly the build dropped me the CS2012 error inform me that it cannot access/update the executable in the /Debug directory.
I tried the following and it didn't work:
Restart
Close VS and open the application again
Build->Clean
I couldn't find any solution in the web so I tried:
1. to move the whole project directory to another location.
2. creating a new WPF project and assembling it file by file with copy/pastes.
The weird thing was that in both cases the application worked in the new location. I continued the experimentation (in the original directory) and i tried to build a "Hello World" Console application in the same directory. The result was that the trivial console application didn't work and produced the same problem as the WPF application (CS2012 error).
Since I haven't noticed any activity of another program (i.e., antivirus) trying to quarantine (or changing the file/folder permissions of) this folder, I assume that this has been done by VS somehow but I don't know why. Perhaps it is a bug.
Is anyone has a logical explanation about this problem? And a way to fix it?
I found that my other running solution was referencing the same < executable path>.
Just make sure that no other process is using the referenced folder/file/dll
Try to close all processes, move the project to a different folder (on a different disk) b restart the computer and everything will work as it should. It worked for me without any problems. Hope this helps someone
I wanted to quickly test something in a .NET Core Console Application Solution and ran into this issue due to BitDefender blocking the resulting binaries.
I've named the app client which actually was the culprit. Renaming my solution fixed this.
Sometimes antivirus softwares can block copying an exe file from a folder to any path. You can manage by settings or the easiest way is to shut down live protection while you are coding and debugging.
:)
Deactivate your anti-virus for a while and try again.
This works for me.

WPF application installed with Setup project crashes

I have created a Setup file of my WPF application using Visual Studio setup project.
The setup seems installing ok and I get the installed folder and all the files that are intended to be there.
Now when I run the exe I get following error screen.
Please note when i run the application it runs pretty wel without any issues.
Please note I am using EntityFramework,WPFToolkit and log4net with the application.
And I am installing the application in the rot of C drive to avoid any restriction issue writing the log files.
Can anybody please let me know how I can track what is happening ? I tried 'try catch' in App.xaml.cs OnStartUp and MainWindow constractor returning no exceptions.
Following link has the files mentioned in the error screen :
https://www.dropbox.com/sh/2shp428xcawuk8u/mg5cyXvQ_8
My guess is that your application doesn't have write permission in somewhere.
You just need to find out where it is.
The best solution is to add a listener to Application.DispatcherUnhandledException and see what the real error is. It works even better that log4net.

Request for the permission of type 'System.Web.AspNetHostingPermission"

I am facing a problem for running my website.
I have developed the website and when i hit F5 to view the results i am presented with this error
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
How can i solve this problem.
This error usually happens if you Map your application folder on Network Drive or open Solution file from UNC Path. somthing like this:
Invalid Example: \\my-server\my-project
Try to open your solution from direct path
Valid Example: C:\Project\MyProject
You can also make change to your privilege of App Pool Identity but it's not recommanded:
Web Server (IIS) > App Pool Identity > Advance Settings > Set Identity as Network Service
the one shot solution worked for me was to set the App Pool Identity(Advance Settings) to Network Service
Try to set "Load User Profile" value of your application pool to "True".
It worked for me.
For what its worth, I found that the AjaxControlKit.dll on the deployed server was blocked. To resolve this, I copied over the dll to a new folder then copied and pasted in the bin folder. I guess .net doesn't like a dll being copied over directly to the web folder
you can run the following command to Grant FullTrust to remote share:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>caspol.exe -m -ag 1 -url
"file:////\yourcomputername\yoursharename*" FullTrust -exclusive on
Hope this helps.
I tried all of the solutions posted here and a number of other places and nothing worked for me. I had my project located on a network drive which worked until I tried to use the AjaxControlToolkit 2.0 in my project. Apparently, something about permissions on the network drive would not allow the controls in the toolkit to run. I moved my project back to my local hard drive and it all started working.
I had this problem as well but have not seen this particular solution mentioned anywhere.
After checking permissions for the IIS user, the user for Impersonation, and granting Full Trust to the application, and still getting the error, I installed Procmon.exe on the server running IIS and noticed that IIS was attempting to access some Framework64 files for the offending website. I inspected the application pool advanced settings and discovered that "enable 32-bit applications" was set to false. Changing this setting to true proved to be the correct action. Life is good.
I face the same problem, then I realised that I was running the solution from a remote folder. Copy the necessary file to the local folder will solve the issue. But make sure that your folder have the full permission
While all of the other answers on this page may be the cause of anyone's problem, the most common one is due to OS security changes in Windows Server 2012 and above. Any file downloaded from the internet is flagged as suspicious by Windows and needs to be manually unblocked before using it in IIS. So if you FTP your code, email it, or transfer it through a cloud provider, it will be flagged and blocked.
The best solution is to use the web publishing facilities built in to Visual Studio or transfer through a network share if you're on a domain, each of which avoid this problem. However, if you must copy your code to another server by one of the means I described above, you should zip everything prior to transfer so you are only left with a single file to unblock. If you unzip before you unblock, then each file will be blocked and multi-selection will not give you the option to unblock.
So finally... to unblock: Right click the file and select Properties. Right there on the first page near the bottom you'll see an Unblock button along with the warning that the file was downloaded from the internet!
If the dll is compiled using this option (Do not merge. Create a separate assembly for each page and control), please make sure the dll is up to date. I faced the error due to aspx file is new one, but dll still using old one.
I found a potential solution that worked on my system and did not require changing to Network Service.
Go to Application Pool -> Advanced Settings -> Process Model -> Load User Profile: True
Source of this answer.
The best way to resolve this issue is Run the Visual Studio as "Administator". Right click on the Visual Studio icon and select "Run as Administrator".
Once you open Visual Studio with Full admin rights, you will be able access the page without any issues.
I have had this error and it seems to be a generic error. As there are several answers to this issue, I am going to add mine. Republishing the website on the remote server seems to fix it for me.

section registered as allowDefinition='MachineToApplication' beyond application level

I am getting this error when I create a new folder, and upload files to it. I have an existing site that's built, but I don't want to add this to the site, but rather have it be an application all by itself. Any ideas?
This is indeed the first scenario to check. However, the most frustrating scenario in which this error occurs is after a build/publish under a different build configuration (i.e. build under Release, and then trying to build under Debug and getting this error). You should perform Clean operation on the Release mode.
See this answer which points to further details: Error: allowDefinition='MachineToApplication' beyond application level
I just had this problem not too long ago. Go into IIS, select the folder you created and go to properties. You will see "application name" greyed out, click the "create" button and save it. You will be able to run your separate application in it.
I just solved such errors after hours of trying, I had the same scenario resolved by simply deleting any back up files your solution may have created moving your application from one version of visual studio to the other and the problem is resolved
I faced this issue many times. Different solutions worked in different occasions. Following are the solutions.
Create an IIS application for your web site.
Clean the solution and rebuilt again.
Delete the obj folder and rebuilt again.
Open IIS click right click on the folder and Click "Convert to Application"
well i've just resolved this error.
just go to your project/website folder (not the published one). search web.config in the whole folder if it found more than once, you know which to delete. otherwise if only one web.config is found. make sure its has the rights to be Read. and the final problem if everything is fine, then make a new project in visual studio and remove all the components then add existing project by right clicking it, build,debug. Voila!!!
I had this problem when I moved my application from Window XP to Windows 7. The error is primarily because you did not convert your folder into Application.
The mistake that I was making was, when I selected the folder in IIS and right clicked, I choose "Add Application". This should be "Convert to Application". The resolved my problem.
You want to remove the application from your folder then do the process again and select "Convert to Application".
The project folder you are accessing might be the incorrect one Which result in finding two webconfig file for a project. So try to open the correct website.
For what it's worth, while in development, I got this same error when using "Open Website" on a project that I created using "New Project". If I instead open with "Open Project", the error goes away.
I just faced this today...Here is my fix and understanding of it.
Right click on your webproject and 'unload' it.
Edit your projects .csproj file (I assume .vs.proj in vb)
Look for 'true'
Change it to false.
Save your .csproj
Right Click on the .csproj and reload it.
Hopefully fixes your problem.
It's my understanding that the error you are getting is 'real', in my case
in my web.config was causing the error, because this should actually be set on our server, not by the application.
For some reason, reading Phil Haacks post help me to figure this out...I hope this helps others, I was unable to find a solution that met our needs while looking.
If you are getting this error in Visual Studio, Scott Michell has explained it very well.
Basically you are opening the parent folder of the site. In this case web.conf is in subfolder which does not allow certain tags and hence this error. To resolve, simply reopen the site and select the root folder. The error should disappear.
If the error is IIS related, then creating Application for the folder might help.
Open IIS
Select the Application Pool
3.Select pool where application hosted
4.Click on "Advanced Settings"
5.Change the "Managed Pipe Line Mode" to "Integrated"
If your web application is running in the SharePoint hive, remember to copy your application to the correct hive (14 or 15) and server (FE or App) and then create the virtual directory in the SharePoint 80 "_layouts" subfolder.

Categories

Resources