dotnet/wpf · error · IOException

SR.Format(SR.UnableToLocateResource, Uri.ToString())

Error message

SR.Format(SR.UnableToLocateResource, Uri.ToString())

What it means

ContentFilePart.GetStreamCore opens the file backing a content-file package part from the deployment directory. When CriticalOpenFile returns null — the resolved _fullPath does not exist or cannot be opened — the part throws IOException with 'UnableToLocateResource' plus the part URI. This signals that a resource the application manifest claims to include is missing at the expected location.

Solutions

  1. Verify the file referenced by the URI exists at the deployment/application files location and is included in the ClickOnce manifest.
  2. Rebuild and republish the application ensuring the content file is copied to output and marked as Content in the project file.
  3. Clear the ClickOnce cache (mage -cc or delete the app's 2.0 folder under Local Settings Apps) and reinstall.
  4. Catch IOException and fall back to a embedded-resource lookup (GetManifestResourceStream) as a resilience measure.

Example fix

// before
using var s = contentFilePart.GetStream();
// after
if (!File.Exists(resolvedPath))
    throw new FileNotFoundException($"Deployed content file missing: {resolvedPath}");
using var s = contentFilePart.GetStream();
Defensive patterns

Strategy: try-catch

Validate before calling

string path = ResolveDeploymentPath(partUri);
if (string.IsNullOrEmpty(path) || !File.Exists(path))
    throw new FileNotFoundException($"Deployed resource missing: {partUri}", path);

Try / catch

try
{
    stream = part.GetStream();
}
catch (IOException ex) when (ex.Message.Contains("UnableToLocateResource"))
{
    stream = LoadFromEmbeddedResources(partUri);
}

Prevention

When it happens

Trigger: Opening a stream from a ContentFilePart (e.g. via application/resource part APIs on a Package created from a deployed ClickOnce/WPF app) when the file at _fullPath is absent or unreadable.

Common situations: ClickOnce deployment where a content file was not published; application files moved or deleted from the deployment folder; manifest references a file excluded from build output; partial/corrupted deployment cache.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7cfbe64735b2020b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ContentFilePart.cs:75

                string assemblyName, assemblyVersion, assemblyKey;
                string filePath;

                // For now, only Application assembly supports content files, 
                // so we can simply ignore the assemblyname etc.
                // In the future, we may extend this support for regular library assembly,
                // assemblyName will be used to predict the right file path.

                BaseUriHelper.GetAssemblyNameAndPart(Uri, out filePath, out assemblyName, out assemblyVersion, out assemblyKey);

                // filePath should not have leading slash.  GetAssemblyNameAndPart( ) can guarantee it.
                _fullPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(location), filePath);
            }

            stream = CriticalOpenFile(_fullPath);

            if (stream == null)
            {
                throw new IOException(SR.Format(SR.UnableToLocateResource, Uri.ToString()));
            }

            return stream;
        }

        protected override string GetContentTypeCore()
        {
            return MS.Internal.MimeTypeMapper.GetMimeTypeFromUri(new Uri(Uri.ToString(), UriKind.RelativeOrAbsolute)).ToString();
        }

        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------

View on GitHub (pinned to 81131a70a4)