将 .mpp 文件添加到列表 <>
Add .mpp file to list <>
我正在处理 .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;
}
我正在处理 .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;
}