delete a file problem

hi there this is the code :

DirectoryInfo dirInfo = new DirectoryInfo(FilePath);
                  
                  foreach ( FileInfo f in dirInfo.GetFiles() )
                  {
                        if( f.Extension.Equals(".doc") && (f.Name.Equals("Presto.doc") ) )
                        {
                              f.Delete();
                        }

                  }

the FilePath hold this line : @C:\1\Presto.doc
i need to send only the Dir whitout the file name in order the delete will work
how can i remove the Presto.doc from the Dir Path ?
Tech_MenAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
System.IO.Path.GetDirectoryName(FilePath)
0
 
Fernando SotoRetiredCommented:
Hi Tech_Men;

The code you have posted does work, Itested it. What seems to be the problem is the FilePath you are using. It should be the directory name only. For example in your example the FilePath should be @"C:\1" and not @"C:\1\Presto.doc".

Fernando
0
 
Tech_MenAuthor Commented:
i have done it like this :

string Dir=System.IO.Path.GetDirectoryName(FilePath);
                  DirectoryInfo dirInfo = new DirectoryInfo(Dir);

thanks !!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.