Asp.net checks whether folders and files exist

Source: Internet
Author: User

Allow the path parameter to specify relative or absolute path information. The relative path information is interpreted as relative to the current working directory.

Before checking whether the directory exists, Remove trailing spaces from the end of the path parameter.

The path parameter is case insensitive.

If you do not have the minimum read-only permission for this directory, the exists method returns false.

If directory. exists (path) then
'This path is a directory.
Processdirectory (path)
Else
Console. writeline ("{0} is not a valid file or directory.", path)
End if

Next, let's look at a detailed instance.


// Determine the existence, creation, and deletion of folders
String aaaa = "f: notebookhaha"; // the correct path
If (directory. exists (aaaa) // create a file folder if it does not exist.
{
Messagebox. show ("folder ");
// Directory. delete (aaaa, false); // if a file or directory exists in the folder, an error is returned.
// Directory. delete (aaaa, true); // true indicates that the folder and Its subdirectories and files are deleted.
}
Else
{
Messagebox. show ("Folder does not exist ");
Directory. createdirectory (aaaa); // create the folder
}

// Determine the existence, creation, and deletion of an object
String dddd = aaaa + "11.txt ";
If (file. exists (dddd ))
{
Messagebox. show ("file exists ");
File. delete (dddd); // delete the file
}
Else
{
Messagebox. show ("file does not exist ");
File. create (dddd); // create the file. If the path Folder does not exist, an error is returned.
}

<Span style = "font-family: verdana, 'courier new'" face = "verdana, 'courier new'"> <span style = "font-size: 15px; line-height: 18px; white-space: normal ">
</Span>

If a friend says file. exists () is used to map files on the network disk, no matter whether the files exist or not, false is returned. For details, see the following description.


File. exists () itself. You can try the same function in winform without any errors.
The reason for sharing files on other machines in asp tutorial. net is that false is returned regardless of whether the files exist or not, mainly because of user permissions. When running

In the asp.net tutorial program, the current user permission is not used for running, but the aspnet user permission. In terms of security, this permission is

Editions have strict restrictions. If the aspnet permission does not allow you to view the shared file, false is returned.

There is a workaround: You can change the user permissions of the asp.net program, and specify

Specific users:
<Identity impersonate = "true" username = "accountname" password = "password"/>
After testing, you should be able to directly detect the existence of shared files on other machines:
Bool a = file. exists ("othercomputersharetest.txt ");
(Check whether the files on the network ing disk still have problems ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.