在列表框(Asp.net c#)中显示目录中的word文件列表
在按钮单击事件中从本地计算机上的目录打开word文件的我的代码:在列表框(Asp.net c#)中显示目录中的word文件列表,c#,asp.net,io,listbox,directory,C#,Asp.net,Io,Listbox,Directory,在按钮单击事件中从本地计算机上的目录打开word文件的我的代码: `string path = @"C:\Users\Ansar\Documents\Visual Studio 2010\Projects\GoodLifeTemplate\GoodLifeTemplate\Reports\IT"; List<string> AllFiles = new List<string>(); protected void Page_Load(object
`string path = @"C:\Users\Ansar\Documents\Visual Studio 2010\Projects\GoodLifeTemplate\GoodLifeTemplate\Reports\IT";
List<string> AllFiles = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ParsePath();
}
}
void ParsePath()
{
string[] SubDirs = Directory.GetDirectories(path);
AllFiles.AddRange(SubDirs);
AllFiles.AddRange(Directory.GetFiles(path));
int i = 0;
foreach (string subdir in SubDirs)
{
ListBox1.Items.Add(SubDirs[i].Substring(path.Length + 1, subdir.Length - path.Length - 1).ToString());
i++;
}
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] Files = d.GetFiles("*.doc")
ListBox1.Items.Clear();
foreach (FileInfo file in Files)
{
ListBox1.Items.Add(file.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
Document document = ap.Documents.Open(path + "\\" + ListBox1.SelectedItem.Text);
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "error", "Please select file;", true);
}
`string path=@“C:\Users\Ansar\Documents\visualstudio 2010\Projects\GoodLifeTemplate\GoodLifeTemplate\Reports\IT”;
List AllFiles=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
ParsePath();
}
}
void ParsePath()
{
字符串[]SubDirs=Directory.GetDirectories(路径);
AllFiles.AddRange(子目录);
AddRange(Directory.GetFiles(path));
int i=0;
foreach(子目录中的字符串子目录)
{
ListBox1.Items.Add(SubDirs[i].Substring(path.Length+1,subdir.Length-path.Length-1.ToString());
i++;
}
DirectoryInfo d=新的DirectoryInfo(路径);
FileInfo[]Files=d.GetFiles(“*.doc”)
ListBox1.Items.Clear();
foreach(文件中的文件信息文件)
{
ListBox1.Items.Add(file.ToString());
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
if(ListBox1.SelectedItem!=null)
{
Microsoft.Office.Interop.Word.Application ap=新的Microsoft.Office.Interop.Word.Application();
Document Document=ap.Documents.Open(路径+“\\”+列表框1.SelectedItem.Text);
}
其他的
{
ScriptManager.RegisterStartupScript(此,GetType(),“错误”,“请选择文件;”,true);
}
`
这是在列表框中显示所有word文件列表,正如我的要求一样,但也是以前打开的临时word文件,即
(~$nameoffile.docx)
我不想在列表框中显示此(~$nameoffile.docx)
。文件~$[something].docx
是隐藏文件。我建议您做的是确保将它们过滤掉,如:
System.IO.DirectoryInfo dirInf = new System.IO.DirectoryInfo(@"C:\myDir\Documents");
var files = dirInf.GetFiles("*.doc").Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden).ToArray();
dirInf.GetFiles
的搜索模式与windows的工作方式相同
这是一个:
.Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden)
(f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden
这是一个:
.Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden)
(f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden