Matters need to pay attention to System.IO.File.exists

xiaoxiao2021-03-06  79

You:

When the .NET Framework is in design, he has not completely throwing an exception, which may have a lot of unexpected problems.

such as

You determine if the file exists in the ASP.NET application, this file may be a shared path, such as system.io.file.exists (// Montaquehou-mis / share / a.file)

This file can be accessed in the Explorer, but it is generally unacceptable in your application. At this time, File.exists will return false, exist when the file is present.

The reason is very simple, ASP.NET default is the ASPNET user of this unit, this user has no access.

Let's take a look at the internal implementation of File.Exists, (System.IO.File class is placed in mocorlib.dll, you can use tools similar to the Reflector to compile))

Public Static Bool EXISTS (String Path) {string [] textArray1; try {if (path == null) {Return False;} if (path.length == 0) {Return False;} Path = path.getFullPathinternal (PATH) TextArray1 = new string [1]; TextArray1 [0] = path; New fileiopermission (1, TextArray1, 0, 0) .demand (); // Explicit requirement a permission return file.internalexists (PATH);} catch (ArgumentException) {} catch (NotSupportedException) {} catch (SecurityException) // after an exception is not thrown {} catch (IOException) {} catch (UnauthorizedAccessException) {} return false; // this is what you see false }

转载请注明原文地址:https://www.9cbs.com/read-121330.html

New Post(0)