我正在使用.mpp文件...我要做的第一件事是读取扩展名为.mpp的文件,这对我来说很好,现在我想做的就是将该文件添加到列表中,但是我无法获取。
我是这个新事物
private void Btn_Actualizar_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
if (theDialog.ShowDialog() == DialogResult.OK)
{
string fileName = theDialog.FileName.ToString();
Load(fileName);
}
}
public new string Load(string fileName)
{
MSProject.ApplicationClass app = null;
string retVal = "";
List<Task> tasks = new List<Task>();
try
{
// execute the Microsoft Project Application
app = new MSProject.ApplicationClass();
// Do not display Microsoft Project
app.Visible = false;
// open the project file.
if (app.FileOpen(fileName, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
{
// go through all the open projects--there should only be one
foreach (MSProject.Project proj in app.Projects)
{
// go through all the tasks in the project
foreach (MSProject.Task task in proj.Tasks)
{
// add Microsoft Project Task to arraylist
//tasks.Add(new Task(task));
//tasks.Add(task);
List<Task> tasks1 = new List<Task>();
}
}
}
else
{
retVal = "The MS Project file " + fileName + " could not be opened.";
}
}
catch (Exception ex)
{
retVal = "Could not process the MS Project file " + fileName + "." + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace;
}
// close the application if is was opened.
if (app != null)
{
app.Quit(MSProject.PjSaveType.pjDoNotSave);
}
return retVal;
}
我要在其中添加读取文件结果的位置在我的代码的此部分中
foreach (MSProject.Task task in proj.Tasks)
{
// add Microsoft Project Task to arraylist
//tasks.Add(new Task(task));
//tasks.Add(task);
List<Task> tasks1 = new List<Task>();
}
搜索网络已经尝试了几种方法,但到目前为止,它对我来说不起作用,列表始终为0
你是如此亲密。不幸的是,在循环中,您一直在重新创建任务列表,而不是一次创建并将每个Task
添加到现有List
中。
//First, declare your task list outside of any of your event handlers
List<MSProject.Task> tasks = new List<MSProject.Task>();
private void Btn_Actualizar_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
// ... and the rest of that code
在Load()
函数中,像这样更改代码,请尝试以下方法:
public new string Load(string fileName)
{
MSProject.ApplicationClass app = null;
string retVal = "";
//if the list object hasn't been created yet, then create it now
if (tasks == null)
tasks = new List<MSProject.Task>();
else //if it was already created, just clear it.
tasks.Clear();
try
{
//... (I didn't paste all of the code. It is still required, of course.)
// go through all the open projects--there should only be one
foreach (MSProject.Project proj in app.Projects)
{
// go through all the tasks in the project
foreach (MSProject.Task task in proj.Tasks)
{
// add Microsoft Project Task to arraylist
tasks.Add(task); //<-- this was the correct line, uncommented
}
//... (I didn't paste all of the code. It is still required, of course.)
@@ Michael Jones也有很好的建议。一些库重新使用了名称Task
。最好包含名称空间MSProject
,以避免任何歧义和相关错误。
根据我的评论,更改声明列表的方式,然后取消对添加内容的注释。现在,您只需要弄清楚列表的实际用途。
private void Btn_Actualizar_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
if (theDialog.ShowDialog() == DialogResult.OK)
{
string fileName = theDialog.FileName.ToString();
Load(fileName);
}
}
public new string Load(string fileName)
{
MSProject.ApplicationClass app = null;
string retVal = "";
List<MSProject.Task> tasks = new List<MSProject.Task>();
try
{
// execute the Microsoft Project Application
app = new MSProject.ApplicationClass();
// Do not display Microsoft Project
app.Visible = false;
// open the project file.
if (app.FileOpen(fileName, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing))
{
// go through all the open projects--there should only be one
foreach (MSProject.Project proj in app.Projects)
{
// go through all the tasks in the project
foreach (MSProject.Task task in proj.Tasks)
{
// add Microsoft Project Task to arraylist
//tasks.Add(new Task(task));
tasks.Add(task);
//List<Task> tasks1 = new List<Task>();
}
}
}
else
{
retVal = "The MS Project file " + fileName + " could not be opened.";
}
}
catch (Exception ex)
{
retVal = "Could not process the MS Project file " + fileName + "." + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace;
}
// close the application if is was opened.
if (app != null)
{
app.Quit(MSProject.PjSaveType.pjDoNotSave);
}
return retVal;
}