我的代码,用于在按钮单击事件上从本地计算机上的目录中打开word文件:
`string path = @"C:UsersAnsarDocumentsVisual Studio 2010ProjectsGoodLifeTemplateGoodLifeTemplateReportsIT";
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);
}
'
这是我的要求,这在列表框中显示所有word文件列表,但也显示以前打开的临时word文件,(~$nameoffile.docx)
我不想在列表框列表中显示此(~$nameoffile.docx)
。
~$[something].docx
的文件是隐藏文件。我建议您做的是确保像以下那样过滤掉它们:
System.IO.DirectoryInfo dirInf = new System.IO.DirectoryInfo(@"C:myDirDocuments");
var files = dirInf.GetFiles("*.doc").Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden).ToArray();
dirInf.GetFiles
的搜索模式的工作方式与窗口相同。
这是一个 Lambda 表达式:
.Where(f => (f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden)
这是一个按位比较:
(f.Attributes & System.IO.FileAttributes.Hidden) != System.IO.FileAttributes.Hidden