How to fix "The "GenerateResource" task failed unexpectedly"

By FoxLearn 11/10/2024 11:14:35 PM   61
The error "The 'GenerateResource' task failed unexpectedly" in Visual Studio typically occurs when there is an issue with the resource generation process, usually related to embedded resources or .resx files.

If you get an error as shown below.

The "GenerateResource" task failed unexpectedly.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Build.Tasks.ResGenDependencies.GetResXFileInfo(String resxFile)
   at Microsoft.Build.Tasks.GenerateResource.ShouldRebuildResgenOutputFile(String sourceFilePath, String outputFilePath)
   at Microsoft.Build.Tasks.GenerateResource.GetResourcesToProcess(List`1& inputsToProcess, List`1& outputsToProcess, List`1& cachedOutputFiles)
   at Microsoft.Build.Tasks.GenerateResource.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext() MyApp

or

The "GenerateResource" task failed unexpectedly.
System.Runtime.InteropServices.COMException (0x80070050): The file exists. (Exception from HRESULT: 0x80070050)
   at Microsoft.Build.Shared.NativeMethodsShared.InprocTracking.WriteContextTLogs(String intermediateDirectory, String tlogRootName)
   at Microsoft.Build.Utilities.FileTracker.WriteContextTLogs(String intermediateDirectory, String taskName)
   at Microsoft.Build.Tasks.GenerateResource.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)   ImportProcessorWindowsService

The GenerateResource task often fails because of issues with .resx files, such as incorrect formatting, unsupported characters, or issues with embedded resources.

The issue was caused by certain files being marked as Read-Only in the `obj/x86` folder. To resolve the problem, the Read-Only attribute on those files needed to be cleared.

The final solution was to delete the `.\obj` folder and then rebuild the project, which resolved the issue.