如何在C#中检查文本文件是否在特定路径中生成 [英] How to check if a text file has generated in a particular path or not in C#

查看:83
本文介绍了如何在C#中检查文本文件是否在特定路径中生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个.txt文件将在每次运行生成它的存储过程后动态名称发生变化的路径中导出,该.txt文件的位置在路径下--D:\ work\int \\ \\ retail \ store \出口。现在我想在C#中验证.txt文件是否已经进入此路径。我使用下面的语法根据哪个file.exists()仍然返回false,即使.txt文件在导出位置。这里出了什么问题?任何帮助将在这上得到赞赏。我可以得到最新的文件这个位置动态地进入下面的查询名称?





var FilePath = @D:\work\int \\\tail\store\export;

if(File.Exists(FilePath))

{

//这样做< br $>
}



我尝试过:



我试过像这样获取文件





var FilePath = @D:\work\int\retail \store\export;



.txt文件位于export文件夹中,名称是动态的,每次运行后都会更改,如何获取文件名然后使用file.exists?

one .txt file is getting exported inside the path whose name changes dynamically after each time i run the stored procedure which generates it, the location of that .txt file is under the path - D:\work\int\retail\store\export . Now i want to validate in C# whether the .txt file has come or not in this path. I am using the below syntax according to which file.exists() is still returning false even though .txt file is there in the export location.what's going wrong here?Any help will be appreciated on this.how can i get the latest file name coming in this location dynamically and pass to this below query?


var FilePath = @"D:\work\int\retail\store\export";
if(File.Exists(FilePath))
{
//do this
}

What I have tried:

I have tried fetching the file like this


var FilePath = @"D:\work\int\retail\store\export";

The .txt files comes inside export folder and the name is dynamic which changes after each run, how to get the filename and then use file.exists?

推荐答案

File.Exists是正确的方法;但是,由于您通过ASP.Net运行此程序,运行您的应用程序池的用户帐户是执行代码的实际帐户,该帐户没有权限。



进入IIS并找到您的应用程序池正在使用的身份,然后将其更改为具有权限的帐户,或者您可以为您感兴趣的文件夹授予适当的权限。
File.Exists is the correct method; however, since you are running this through ASP.Net the user account that is running your app pool is the actual account executing the code and that account does not have permissions.

Go into IIS and find what identity your app pool is using and then change it to an account that has permissions or you can grant the proper permissions to the folder that you are interested in.


包含命名空间:使用System.Linq;

Include Namespace: Using System.Linq;
string path= @"D:\DownLoad\";







foreach (string s in Directory.GetFiles(path, "*.txt").Select(Path.GetFileName))
           Console.WriteLine(s);





你将获得所有txt文件名



You will get all txt file name


这篇关于如何在C#中检查文本文件是否在特定路径中生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆