1 把文件资源 比如图片 批量改成可以复制到bin文件里 属性
<ItemGroup>
<Content Include="Resources\b1.jpg" />
<Content Include="Resources\b2.jpg" />
<Content Include="Resources\b3.jpg" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\*.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Resources\*.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
2 使用form的框架类 在高版本里面使用 直接修改.scproj
<UseWindowsForms>true</UseWindowsForms>
3 你的初始化逻辑不依赖 UI 渲染状态、不访问 ActualWidth/Height、不操作可视化树、不需要等窗口显示出来 ,那么放在构造函数中是完全没问题,甚至是推荐的。
4 tools----生成resx
// 获取 .exe 所在目录(例如:bin\Debug)
string outputDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// 跳到项目根目录:假设是 ..\..\
string projectDirectory = Path.GetFullPath(Path.Combine(outputDirectory, @"..\..\.."));
string filePath = Path.Combine(projectDirectory, "AA.resx"); // 推荐 .resx 扩展名
string Resourcpath = projectDirectory + "\\Resources";
// 4. 获取所有图片文件(支持常见格式)
string[] imageFiles = Directory.GetFiles(Resourcpath, "*.*")
.Where(file => new[] { ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".ico" }
.Contains(Path.GetExtension(file).ToLower()))
.ToArray();
if (imageFiles.Length == 0)
{
MessageBox.Show("未在 Images 文件夹中找到图片文件。");
return;
}
if (File.Exists(filePath))
{
File.Delete(filePath);
}
//获取图片的resource
ResXResourceWriter rw = new ResXResourceWriter(filePath);
try
{
foreach (string imageFile in imageFiles)
{
string fileName = Path.GetFileName(imageFile); // 带扩展名:b1.jpg
string resourceName = Path.GetFileNameWithoutExtension(imageFile); // 不带扩展名:b1
if (!fileName.StartsWith("im"))
continue;
try
{
rw.AddResource(resourceName, Image.FromFile(imageFile));
}
catch (Exception ex)
{
MessageBox.Show($"无法加载图片: {fileName}, 错误: {ex.Message}");
}
}
}
finally
{
rw.Generate();
rw.Close();
}