Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:32:03+00:00 2026-06-17T07:32:03+00:00

So if I directly use kernel32 to delete my file as an example, would

  • 0

So if I directly use kernel32 to delete my file as an example, would it be more efficient alternative to this C# API?

System.IO.File.Delete(string Path);

If I import the dll like so and just use DeleteFile(“C:..”):

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFile([MarshalAs(UnmanagedType.LPWStr)]string lpFileName);

Will it be better in effeciency e.g Time it takes to execute the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:32:04+00:00Added an answer on June 17, 2026 at 7:32 am

    It is a fairly common misconception that managed code is slow. In general it is not, you execute the same kind of code that a C compiler produces, machine code. It is especially not relevant with a large number of the kind of .Net framework classes that are wrappers for operating system functions.

    Like the File class. The File.Delete() method in fact pinvokes the DeleteFile() winapi function. Necessarily so, that’s the only way to delete a file on Windows. There is nothing the .NET framework could do, at least not until a managed operating system becomes available. Which may actually happen some day, the super-secret Midori project at Microsoft aims to develop just such an operating system.

    But that’s future-music, right now it is a big chunk of C code, buried in a file system driver like Fat32 or NTFS that actually gets the job done. It operates at disk speeds, DeleteFile() doesn’t return until the file system driver confirmed that the deletion is completed. Which is very, very slow. Like around 50 milliseconds for an old-fashioned hard drive, in the sub-millisecond range for an SSD.

    File.Delete() does add code, it performs lots of error checking. If verifies that the argument you pass isn’t null, checks if your code isn’t running in a sandbox that denies file deletion right, checks if you didn’t specify any characters in the path that are not legal in a file path, converts an incremental path to a full path and checks that it is still a legal path, checks if the path string isn’t too long, converts short MS-Dos 8.3 names to long names if necessary.

    That’s a bunch of work but that’s measured in microseconds. If you pinvoke DeleteFile then you’ll skip that code but you’ll only make it ~1% faster, at best. What you’ll lose is reliability, the guarantee that if you have an oops in your code or data that you are going to be told about it. With a very clear exception message that tells you exactly what is wrong. Extremely valuable because those few microseconds multiplied by a billion is still no match for the amount of time and suffering you’ll expend on debugging a DeleteFile() that didn’t work.

    Don’t do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why can't we directly use this instead of self in the following example? function
Executor seems like a clean abstraction. When would you want to use Thread directly
I want to use the Google Custom Search API (or custom search engine directly)
Why would one use the AlertDialog.Builder class rather than the methods directly available to
It looks like this block directly flushes the output. What's the practical use of
Is there any way to directly use the Win32Native namespace? I would like to
I have seen that you can either do using System.IO and use Path.GetDirectoryName(blah blah)
According to MSDN documentation for RandomNumberGenerator : Application code does not directly use this
I have this string: 101 I want to write it to a file, in
Is it possible to use an std::string for read() ? Example : std::string data;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.