如何获取其他页面的标题?

时间:2019-06-02 11:51:58

标签: c# asp.net

如何访问其他页面的Page.Title(不是当前页面)?

我通过目录名获取所有文件(.aspx),但是获取文件路径后我却不怎么获取Page.Title。

var tempPath = "/Customer/Pages/";
var dirFullPath = Server.MapPath(tempPath);
string[] subdirs = Directory.GetDirectories(dirFullPath);//get all folders this path
for (int i = 0; i < subdirs.Length; i++)
{
    //get all aspx file path this folder
    string[] aspxfiles = Directory.GetFiles(subdirs[i], "*.aspx", SearchOption.AllDirectories);
    foreach (var item2 in aspxfiles)
    {
        //Access to Page.Title item2
    }
}

tnx。

2 个答案:

答案 0 :(得分:0)

您需要递归获取每个文件夹,以便提取目录信息。这是一个例子。该代码创建一个文件夹和子文件夹中所有文件的XML文件。 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace WriteFileNamesXml
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();


        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());


                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                        Console.WriteLine(errorMsg);
                        writer.WriteElementString("Error", errorMsg);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}

答案 1 :(得分:0)

“ Page.Title”属性在运行时由asp编译aspx页面时存在。因此,您应该从带有子字符串“ <title>”和“ </title>”的文件中提取文件;如果它是动态文件,则必须将其存储在某个地方或再次查询。