We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

delete a file problem

Tech_Men
Tech_Men asked
on
Medium Priority
189 Views
Last Modified: 2010-04-16
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 ?
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
System.IO.Path.GetDirectoryName(FilePath)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Author

Commented:
i have done it like this :

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

thanks !!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.