Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call API in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to call API in C #, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

We sometimes need to traverse the files and subdirectories under a certain directory, we can use System.IO.DirectoryInfo.GetDirectories or GetFiles to get all the files and subdirectories under the directory, when there are more contents in this directory, this operation is more time-consuming, and sometimes we just need to know whether there are subdirectories in a directory, which is obviously a waste of time. At this point, it's easy to think of three Win32API functions, FindFirstFile,FindNextFile and FindClose. These three API can be used together to traverse files and subdirectories, and can be stopped at any time to avoid unnecessary operations.

In C #, foreach can be used to traverse a sequence. The object used for traversal must implement the System.Collections.IEnumeable interface, while the internally called traversal must implement System.Collections.IEnumerator. For convenience, we encapsulate it as IEnumerator when we use API functions such as FindFirstFile, and it is actually conditionally encapsulated.

Many people here will mention the execution efficiency of C # calling API, thinking that it is the right way to use C # to call API, while it is a bit creepy to use C # to call API. But in my personal programming experience, there are also a lot of C # calls to API, experience found that the efficiency problem is not big, can be omitted. I just do routine software that runs on the PC. CPU usually exceeds 1GHZ, and there is no need to consider high real-time performance and high efficiency. If you think too much about efficiency, it will increase the consumption of software development. It is unreasonable from the point of view of engineering development management. I should solve the problem of efficiency that is more prominent, and the problem of efficiency that is not prominent and has little impact can only be solved when there is time. Using C # to encapsulate Win32API will inevitably reduce the execution efficiency, but it is convenient and quick to use after encapsulation, which is considered to be correct.

Here, let's talk about the problem of "technical gilding". The so-called technical gilding means that developers excessively pursue the nature of technology in the software development of the project, trying to coat the technology with a golden shell, resulting in an increase in the workload of software development. the lengthening of the project time may lead to the failure of the project. I have suffered from "technical gilding", and now I pursue it in my heart, but in actual development, I often deliberately suppress the pursuit of it.

Now continue to explore the package plan, this package focuses on the implementation of IEnumerator, and IEnumeable is just a package of IEnumerator. The IEnumerator implementation methods Reset, MoveNext, and property Current,Reset methods are used to reset the traversal, MoveNext is used to find the next file or directory, and Current returns the current file or directory.

The traversal should also pay attention to the call to FindClose, which must be called after traversing without finding a file or subdirectory, or if the API function is not called by C #, it will cause a memory leak.

According to the above design, I wrote the following code:

Whether publicclassFileDirectoryEnumerable:System.Collections.IEnumerable {privateboolbolReturnStringType=true; / returns the query result as a string. If true is returned, the current object is returned as a string. / otherwise, it returns System.IO.FileInfo or System.IO.DirectoryInfo type / publicboolReturnStringType {get {returnbolReturnStringType;} set {bolReturnStringType=value;} privatestringstrSearchPattern= "*"; / wildcard of file or directory name / publicstringSearchPattern {get {returnstrSearchPattern } set {strSearchPattern=value;}} privatestringstrSearchPath=null; / search path, must be absolute path / publicstringSearchPath {get {returnstrSearchPath;} set {strSearchPath=value;}} privateboolbolSearchForFile=true; / publicboolSearchForFile {get {returnbolSearchForFile;} set {bolSearchForFile=value;} privateboolbolSearchForDirectory=true; / publicboolSearchForDirectory {get {returnbolSearchForDirectory;} set {bolSearchForDirectory=value } privateboolbolThrowIOException=true; / whether to throw an exception when an IO error occurs / publicboolThrowIOException {get {returnthis.bolThrowIOException;} set {this.bolThrowIOException=value;}} / returns the built-in file and directory traversal object publicSystem.Collections.IEnumeratorGetEnumerator () {FileDirectoryEnumeratore=newFileDirectoryEnumerator (); e.ReturnStringTypetrathis.bolReturnStringTypeType; .SearchForDirectoryDirectoris.bolSearchForForFileDirectory.bolSearchForFileFileis.bolSearchForFile; e.SearchPath=this.strSearchPath MyList.Add (e); returne;} / close the object / close the object / publicvoidClose () {foreach (FileDirectoryEnumeratoreinmyList) {e.Close ();} myList.Clear ();} above is all the content of the article "how to call API in C #". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report