最后我决定走“丑陋但有效”的方式,直接告诉MS Project桌面客户端连接,打开并在本地保存项目,并带有一些C#代码:
private static void start()
{
var objProcess = Process.Start(@"winproj.exe", "/s https://PWA-URL");
}
static void Main(string[] args)
{
var backgroundThread = new Thread(new ThreadStart(start));
backgroundThread.Start();
Thread.Sleep(10000);
ApplicationClass objProject = new ApplicationClass
{
Visible = false
};
object oMissing = System.Reflection.Missing.Value;
object oFile = @"<>\test-project";
object oFormat = "MSProject.mpp";
object oReadOnly = true;
objProject.DisplayAlerts = false;
objProject.FileOpen(oFile, oReadOnly, PjMergeType.pjDoNotMerge, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oFormat, oMissing, PjPoolOpen.pjPoolReadOnly, oMissing, oMissing, oMissing, oMissing);
objProject.FileSaveAs(@"C:\Windows\Temp\LocalCopy.mpp");
objProject.Quit(PjSaveType.pjDoNotSave);
}
Windows 凭据用于连接到 PWA,虽然可以指定不同的用户和密码,但我没有尝试过(ApplicationClass.FileOpen)。
还应该导入 Microsoft.Office.Interop.MSProject.dll 来编译项目。