【问题标题】:Get name of the files present in current project folder获取当前项目文件夹中存在的文件的名称
【发布时间】:2017-01-13 06:50:48
【问题描述】:

我在运行时在 App_Data 文件夹中创建新文件。我的要求是阅读这些文件名并继续进行。 我的代码如下:

var filenames = from fullFilename
                 in Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory + 
                                             "\\App_Data\\", "*.xml")
                select Path.GetFileName(fullFilename);

foreach (string filename in filenames)
{
    System.Diagnostics.Debug.WriteLine("filename -- "+filename);
}

上面的代码说

找不到目录 App_Data。

但这存在于我的项目中。

如何使用此枚举器获取所有文件的名称?

更新:在运行时创建文件时,我使用 HostingEnvironment.MapPath(@"/myproject/") +"/App_Data"。这将在该位置创建文件。但它不允许我从那里阅读。

【问题讨论】:

  • 仅仅因为它存在于您的源代码树中并不意味着它存在于您的可执行文件编译到的应用程序二进制文件夹中。
  • AppDomain.CurrentDomain.BaseDirectory 的值和你想的一样吗?
  • 我想你想要Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)而不是AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\"
  • App_Data 在哪里?是解决方案中的文件夹还是系统文件夹AppData
  • @MohitShrivastava 我试过了,但它仍然给出错误,因为“抛出异常:mscorlib.dll 中的'System.IO.DirectoryNotFoundException'”。这很明显,因为这给出的路径为“C:\Users\User1\AppData\Roaming”,这是不正确的。这个 App_Data 在我的项目中。

标签: c#


【解决方案1】:

实际上AppDomain.CurrentDomain.BaseDirectory 为您提供当前工作目录,在调试时它为您提供路径Your Projectfolder\\bin\\Debug\\。而您想在 Project 文件夹中找到 App_Data 文件夹,而不是在 debug 中,因此您必须回到 Project 文件夹,然后找到 App_Data,如下所示:

string projectFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName;
string folderAppData = Path.Combine(projectFolder, "App_Data");
if (Directory.Exists(folderAppData))
{
    foreach (string file in Directory.EnumerateFiles(folderAppData,"*.xml"))
    {
           // Do your stuff here
    }
}

【讨论】:

  • 谢谢。但问题在于迭代该文件夹内的文件。我可以使用 HostingEnvironment.MapPath(@"/myproject/") +"/App_Data" 找到路径,因为第一步我已经在该文件夹中创建文件。在读取文件时,它会抛出异常“找不到路径 C:\Users\User1\Workspace\Myproject\App_Data 的一部分”
  • 如果是web应用,使用Server.MapPath
【解决方案2】:

如果您添加 using System.IO 或对其的引用,这应该会在您的应用程序环境中的 App_Data 文件夹中找到所有 .xml 文件:

        DirectoryInfo di = new DirectoryInfo(Server.MapPath("/App_Data"));
        var fi = di.EnumerateFiles("*.xml", SearchOption.TopDirectoryOnly);

// fi should contain the enumeration of files found at that path
// you can iterate over fi and have all the file info of each file found

[更新]

如果您已经解析了路径字符串,则可以将整个 Server.MapPath("/App_Data") 替换为桌面应用程序的路径,其余部分保持不变。

要遍历集合,只需使用 foreach 循环,如下所示:

            foreach (FileInfo file in fi)
            {
                string s = "File Name: " + file.Name + "\r";
                s += "Full Path: " + file.FullName + "\r";
                s += "File Ext.: " + file.Extension + "\r";
                MessageBox.Show(s);
            }

[更新 2]

由于 OP 指的是网络场景

通过将 using System.Web.Hosting 语句添加到我的控制器,我能够在 mvc 示例应用程序中测试“HostingEnvironment.MapPath”。 这是我想出的。

此示例定位虚拟路径“/App_Data”,收集它在路径中找到的“.xml”文件。 然后它遍历集合并将每个文件的内容读入一个数组元素。 最后,它将文件名显示为视图中的链接,以及带有标题#的每个文件的内容。 请记住,这可能不是最优化的代码示例。

C# 代码:在 mvc 控制器中

        DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath("/App_Data"));
        var fi = di.EnumerateFiles("*.xml", SearchOption.TopDirectoryOnly);
        ViewBag.Files = fi;
        string[] fs = new string[fi.Count()];
        int fc = fi.Count();
        int x = 0;
        foreach (FileInfo f in fi)
        {
            StreamReader sr = new StreamReader(f.FullName);
            fs[x] = sr.ReadToEnd();
            sr.Close();
            if (x < fc) x++;
        }
        ViewBag.Contents = fs;
        ViewBag.ContCount = fc;

HTML:在视图中

<div>
    <h3>Files Found:</h3>
    <p>
        @foreach (FileInfo f in @ViewBag.Files)
        {
            <a href="@f.FullName">@f.Name</a><br />
        }

    </p>

</div>
<div>
    <h3>File Contents:</h3>
    @for (int c = 0; c < @ViewBag.ContCount; c++)
    {
        <h4>Heading @(c)</h4>
        <p>
            @ViewBag.Contents[@c]
        </p>
        <hr />
    }
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 2018-05-29
    • 2015-04-10
    • 1970-01-01
    • 2015-06-27
    • 2014-12-09
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多