答案很简单。它应该工作。但事实并非如此。
Visual Studio 用于生成 CAB 文件的 Microsoft 工具 CABWiz 显然存在一个错误。在不同的子文件夹中使用同名文件时会出现问题,例如使用本地化时。
经过数小时的尝试修复它,我最终得到了一个受 Cornel 在上一个答案中给出的 CodeProject 指南启发的解决方案:您必须通过使用具有唯一名称的资源文件来“破解”生成 CAB 的 Visual Studio 过程,然后修改 INF 文件以指定在设备上部署的原始名称。
为了更加自动化,我制作了一个作为项目 post-build 启动的小 EXE:
FileInfo CurrentExeInfo = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
// Current Folder + bin\Debug
DirectoryInfo BinDebug = new DirectoryInfo( Path.Combine( CurrentExeInfo.Directory.FullName, @"bin\Debug") );
// Subfolders in \bin\Debug
Console.WriteLine(BinDebug.FullName);
string[] Dirs = Directory.GetDirectories(BinDebug.FullName, "*", SearchOption.TopDirectoryOnly);
// In each localization folder ...
foreach (string Dir in Dirs)
{
DirectoryInfo DirInfo = new DirectoryInfo(Dir);
// ... Resource files
string[] RFiles = Directory.GetFiles(Dir, "*.resources.dll");
foreach (string RFile in RFiles)
{
FileInfo RFileInfo = new FileInfo(RFile);
bool DoCopy = false;
// No underscore in resource name
if (!RFileInfo.Name.Contains("_") || RFileInfo.Name.IndexOf("_") == 0)
{
DoCopy = true;
}
// underscore in resource name
// --> Have to check if already a copy
else
{
// prefix removal
int PrefixIndex = RFileInfo.Name.IndexOf("_");
string TestFilename = RFileInfo.Name.Substring(PrefixIndex + 1);
if (!File.Exists(Path.Combine(Dir, TestFilename)))
{
// File without underscore does not exist, so must copy
DoCopy = true;
}
}
if (DoCopy)
{
// Copy file
string NewFileName = Path.Combine(Dir, DirInfo.Name.ToUpper() + "_" + RFileInfo.Name);
Console.WriteLine("Copying " + RFile + " -> " + NewFileName);
File.Copy(RFile, NewFileName, true);
}
}
}
然后是正常 CAB 生成后的这个 CAB 修补程序:
const string cabwizpath = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\SmartDevices\SDK\SDKTools\cabwiz.exe";
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Aborted: You must enter the inf file information");
Console.ReadLine();
return;
}
if (!File.Exists(args[0]))
{
Console.WriteLine("Aborted: I can not found the INF file!");
Console.ReadLine();
return;
}
// string to search
Regex R = new Regex("\"[A-Z]{2,3}_(.+)\\.resources\\.dll\",\"([A-Z]{2,3})_(.+)\\.resources\\.dll\"");
// File reading
string inffile = File.ReadAllText(args[0]);
// Format replace from
// "FR_ProjectName.resources.dll","FR_ProjectName.resources.dll"
// To
// "ProjectName.resources.dll","FR_ProjectName.resources.dll"
inffile = R.Replace(inffile, "\"$1.resources.dll\",\"$2_$3.resources.dll\"");
// Rewriting file
File.WriteAllText(args[0], inffile);
Console.WriteLine("INF file patched ...");
// Génération du CAB ...
Console.WriteLine("Generating correct CAB ... ");
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo("\"" + cabwizpath + "\"", "\"" + args[0] + "\"");
proc.ErrorDialog = true;
proc.UseShellExecute = false;
proc.RedirectStandardOutput = true;
Process CabWiz = Process.Start(proc);
Console.WriteLine("\""+cabwizpath + "\" \""+ args[0]+"\"");
CabWiz.WaitForExit();
Console.WriteLine("CAB file generated (" + CabWiz.ExitCode + ") !");
}
我希望它有所帮助。
有关此的更多链接: