You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于绝对路径的两个基础问题:embedded Resource路径及构建后访问疑问

Hey there, let's tackle your two questions clearly and practically:

1. 如何获取Embedded Resource中文件的绝对路径?

First off, a critical clarification: 嵌入资源(Embedded Resource)并不是独立的物理文件 —— it gets compiled directly into your assembly (either .exe or .dll). So there's no such thing as an "absolute path" for it, since it doesn't exist as a separate file on disk.

Instead of chasing a path, you should read the resource's content directly from the assembly. Here's a C# example to demonstrate:

Suppose you've marked data.txt as an embedded resource, and its full resource name is MyApp.Resources.data.txt (usually your project's root namespace plus the folder structure where the file lives):

var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var resourceName = "MyApp.Resources.data.txt";

using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
{
    if (resourceStream == null)
        throw new FileNotFoundException("Embedded resource not found", resourceName);
    
    // Read the content from the stream, e.g. convert to string
    using (StreamReader reader = new StreamReader(resourceStream))
    {
        string content = reader.ReadToEnd();
        // Process the content as needed
    }
}

If you absolutely need a "path" to work with APIs that require one, you can extract the embedded resource to a temporary file and use that temp file's path:

string tempFilePath = Path.GetTempFileName();
// Write the embedded resource's content to tempFilePath here
// Use tempFilePath for your API call
// Don't forget to delete the temp file after use!

2. 若将图片存储在文件夹中并使用绝对路径访问,构建解决方案后是否无法找到这些图片?

You're spot on — this will almost always cause issues after building. The problem is that the absolute path you use during development (like C:\MyDevProject\Images\logo.png) is tied to your local machine's file structure. When you deploy the built solution to another machine (even a different folder on your own), that exact path won't exist, or the image file won't be present there.

Here are three solid fixes for this:

  • Use relative paths + copy to output directory: In Visual Studio, right-click your image file → go to Properties → set "Copy to Output Directory" to "Copy if newer" or "Copy always". This ensures the image gets copied to the output folder's corresponding subdirectory. Then access it with a relative path like ./Images/logo.png.

  • Convert images to embedded resources: Just like the first question's approach, mark the images as embedded resources and read their streams directly from the assembly. This eliminates dependency on external files entirely, making deployment cleaner.

  • Build paths using the app's base directory: Use AppDomain.CurrentDomain.BaseDirectory to get the root folder where your app is running, then append the image's relative path to it:

string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "logo.png");

This way, the path will always point to the correct location regardless of where you deploy the app.


内容的提问来源于stack exchange,提问作者Jieke Wei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:14:34