Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted
Updated 21-Jul-16 2:49am
Comments
[no name] 20-Jul-16 15:34pm    
And where is the filename in your variable FilePath?

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.
 
Share this answer
 
Comments
Richard Deeming 21-Jul-16 9:36am    
It would be the correct method, except I suspect the FilePath variable contains a directory path, not a file path. :)
ZurdoDev 21-Jul-16 9:40am    
Good point. I didn't look closely enough. However, it seems like it will work on a folder as well, from previous experience. Could be wrong though.
Richard Deeming 21-Jul-16 9:42am    
If you pass a directory path to File.Exists, it will return false because the path isn't a file.

The same thing happens if you pass a file path to Directory.Exists.
Include Namespace: Using System.Linq;
C#
string path= @"D:\DownLoad\";


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


You will get all txt file name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900