Solved

What statement would you code to delete all files in the following directory that have a file extension of *.TIF ?

Posted on 2016-09-08
3
61 Views
Last Modified: 2016-09-09
I am writing a C# application using VS2010 and .Net Framework 4.0.

What statement would you code to delete all files in the following directory that have a file extension of *.TIF ?

I use the following value for the directory path:

const string TARGET_DIR_IN = @"\\v\region\na\control\dev\W\data\Test1\";
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41790721
Hi,
pls try

DirectoryInfo di = new DirectoryInfoTARGET_DIR_IN);
FileInfo[] files = di.GetFiles("*.TIF")
                     .Where(p => p.Extension == ".TIF").ToArray();
foreach (FileInfo file in files)
    try
    {
        file.Attributes = FileAttributes.Normal;
        File.Delete(file.FullName);
    }
    catch { }

Open in new window

Regards
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 41791252
Or simply LINQ:

Directory.EnumerateFiles(TARGET_DIR_IN, "*.TIF")
         .ToList()
         .ForEach(File.Delete);

Open in new window

0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41791254
Correct code
DirectoryInfo di = new DirectoryInfo(TARGET_DIR_IN);
FileInfo[] files = di.GetFiles("*.TIF")
                     .Where(p => p.Extension == ".TIF").ToArray();
foreach (FileInfo file in files)
    try
    {
        file.Attributes = FileAttributes.Normal;
        File.Delete(file.FullName);
    }
    catch { }

Open in new window

0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question