C # file/folder operations

Source: Internet
Author: User

Folder where the current program is located
System. Io. Directory. getcurrentdirectory ()

Display files in a specified folder
If (this. textbox1.text. Trim () = "")
Return;
This. listbox1.items. Clear ();
String [] myfiles = system. Io. Directory. getfiles (this. textbox1.text );
This. listbox1.items. addrange (myfiles );

Show subfolders in a specified folder
If (this. textbox1.text. Trim () = "")
Return;
This. listbox1.items. Clear ();

Obtains all subfolders in a specified folder.
String [] myfolders = system. Io. Directory. getdirectories (this. textbox1.text );
This. listbox1.items. addrange (myfolders );

Displays subfolders and files in the specified folder.
If (this. textbox1.text. Trim () = "")
Return;
This. listbox1.items. Clear ();

Obtains all files and subfolders in a specified folder.
String [] myfoldersfiles = system. Io. Directory. getfilesystementries (this. textbox1.text );
This. listbox1.items. addrange (myfoldersfiles );

File Creation Time
This. datetimepicker1.text = file. getcreationtime (this. textbox1.text). tolongdatestring ();

Last modification time
This. datetimepicker2.text = file. getlastwritetime (this. textbox1.text). tolongdatestring ();

Last access time
This. datetimepicker3.text = file. getlastaccesstime (this. textbox1.text). tolongdatestring ();
Fileattributes myattributes = file. getattributes (this. textbox1.text );
String myfiletype = myattributes. tostring ();
If (myfiletype. lastindexof ("readonly ")! =-1) // whether the file is read-only
{
This. checkbox1.checked = true;
}
If (myfiletype. lastindexof ("system ")! =-1) // whether the system file is used
{
This. checkbox2.checked = true;
}
If (myfiletype. lastindexof ("hidden ")! =-1) // hide the object
{
This. checkbox3.checked = true;
}
If (myfiletype. lastindexof ("ARCHIVE ")! =-1) // whether to archive the file
{
This. checkbox4.checked = true;
}
If (myfiletype. lastindexof ("temporary ")! =-1) // whether the file is temporary
{This. checkbox5.checked = true;
}

Set file Properties
If (this. textbox1.text. Length <2)
Return;
File. setattributes (this. textbox1.text, fileattributes. Normal );
If (this. checkbox1.checked = true)
{
File. setattributes (this. textbox1.text, fileattributes. readonly );
}
Fileattributes myattributes = file. getattributes (this. textbox1.text );
If (this. checkbox2.checked = true)
{
File. setattributes (this. textbox1.text, myattributes | fileattributes. system );
}
Myattributes = file. getattributes (this. textbox1.text );
If (this. checkbox3.checked = true)
{
File. setattributes (this. textbox1.text, myattributes | fileattributes. Hidden );
}
Myattributes = file. getattributes (this. textbox1.text );
If (this. checkbox4.checked = true)
{
File. setattributes (this. textbox1.text, myattributes | fileattributes. Archive );
}
Myattributes = file. getattributes (this. textbox1.text );
If (this. checkbox5.checked = true)
{
File. setattributes (this. textbox1.text, myattributes | fileattributes. Temporary );
}
File. setcreationtime (this. textbox1.text, this. datetimepicker1.value );
File. setlastwritetime (this. textbox1.text, this. datetimepicker2.value );
File. setlastaccesstime (this. textbox1.text, this. datetimepicker3.value );
MessageBox. Show ("File Attribute setting operation successful! "," Information prompt ",*******);

Get folder Properties
If (this. textbox1.text. Length <2)
Return;

Get the Folder creation time
This. datetimepicker1.text = directory. getcreationtime (this. textbox1.text). tolongdatestring ();

Obtain the last modification time of a folder.
This. datetimepicker2.text = directory. getlastwritetime (this. textbox1.text). tolongdatestring ();

Obtain the last access time of a folder.
This. datetimepicker3.text = directory. getlastaccesstime (this. textbox1.text). tolongdatestring ();

Get folder Properties
Fileattributes myattributes = file. getattributes (this. textbox1.text );
String myfiletype = myattributes. tostring ();
If (myfiletype. lastindexof ("hidden ")! =-1)
{

Determine hidden folder attributes
This. checkbox3.checked = true;
}
If (myfiletype. lastindexof ("ARCHIVE ")! =-1)
{

Determine the folder archive attributes
This. checkbox4.checked = true;
}

Set folder Properties
If (this. textbox1.text. Length <2)
Return;

Set the folder property to normal
File. setattributes (this. textbox1.text, fileattributes. Normal );
Fileattributes myattributes = file. getattributes (this. textbox1.text );
If (this. checkbox3.checked = true)
{

Set folder hide attributes
File. setattributes (this. textbox1.text, myattributes | fileattributes. Hidden );
}
Myattributes = file. getattributes (this. textbox1.text );
If (this. checkbox4.checked = true)
{

Set folder archive attributes
File. setattributes (this. textbox1.text, myattributes | fileattributes. Archive );

Set the Folder creation time
Directory. setcreationtime (this. textbox1.text, this. datetimepicker1.value );

Set the last modification time of the folder.
Directory. setlastwritetime (this. textbox1.text, this. datetimepicker2.value );

Set the last access time of the folder
Directory. setlastaccesstime (this. textbox1.text, this. datetimepicker3.value );
MessageBox. Show ("folder attribute setting operation successful! "," Information prompt ",*******);

Determine whether a file already exists
String myfilename = This. textbox1.text;
If (myfilename. Length <1)
Return;
String shortname = myfilename. substring (myfilename. lastindexof ("//") + 1 );
If (file. exists (myfilename ))
{
MessageBox. Show ("file:" + shortname + "already exists! "," Message prompt ",*****);}
Else
{
MessageBox. Show ("file:" + shortname + "does not exist! "," Information prompt ",*****);
}

Determine whether a folder already exists
String myfoldername = This. textbox2.text;
If (myfoldername. Length <1)
Return;
String Foldername = myfoldername. substring (myfoldername. lastindexof ("//") + 1 );
If (directory. exists (myfoldername ))
{
MessageBox. Show ("folder:" + Foldername + "already exists! "," Information prompt ",*****);
}
Else
{
MessageBox. Show ("folder:" + Foldername + "does not exist! "," Information prompt ",*****);
}

Delete folder
If (this. textbox1.text. Trim () = "")
Return;
Directoryinfo mydir = new directoryinfo (this. textbox1.text );
If (MessageBox. Show ("delete Folder:" + this. textbox1.text + "and all its contents? "," Prompt ", messageboxbuttons. yesno, messageboxicon. Question) = dialogresult. Yes)
{
Mydir. Delete (true );
This. textbox1.text = "";
}

Create multi-layer folders
If (this. textbox2.text. Trim () = "")
Return;
If (directory. exists (this. textbox2.text ))
{
MessageBox. Show ("this folder already exists! "," Prompt ", messageboxbuttons. OK, messageboxicon. information );
Return;
}
If (MessageBox. Show ("whether to create a multi-layer Folder:" + this. textbox2.text, "prompt", messageboxbuttons. yesno, messageboxicon. Question) = dialogresult. Yes)
{
Directory. createdirectory (this. textbox2.text );
This. textbox2.text = "";
}

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.