Home > Net >  C # file operations consult these two methods have concise writing
C # file operations consult these two methods have concise writing

Time:09-22

Method one: GetFileNames (string filePath)
Obtain the specified path folder (filePath such as c: \ Files) all file names (as long as the name, don't bring path)
Method 2: used by CopyFile (string path1, string path2)
Copy the specified File (a single File, the path is path1, such as c: \ a.t xt), to the target folder path (path2, such as d: \ File),




Search on the Internet for a day, didn't find a suitable,
New small white, the great god give directions,

CodePudding user response:

Var file name=System. IO. Path. GetFileName (complete file Path);

CodePudding user response:

File. Copy (path1, Path.Com bine (@ "d: \ File, File name));

CodePudding user response:

Didn't understand
The somebody else write good function, how to simplify

CodePudding user response:

 public static string [] take all folder name folder (string) 
{
Return to the Directory
GetFiles (folder)
The Select (x=& gt; Path. The GetFileName (x))
The ToArray ();
}

CodePudding user response:

refer to autumn red fruit reply: 3/f
understand
People write good function, how to simplify the

Not written is my naming method

CodePudding user response:

reference 5 floor ljg3081022a reply:
Quote: refer to the third floor of autumn red fruit response:
understand
People write good function, how to simplify the

Not written is my method named

The original is such, it can be
 
Private List GetFileNames (string filePath)
{
Return new DirectoryInfo (filePath.) GetFiles (). The Select (f=& gt; The cash-strapped ame). ToList ();
}

Private void used by CopyFile (string path1, string path2)
{
File. Copy (path1, path2 + "\ " + path1. Substring (path1. LastIndexOf (' \ \ ') + 1), false);
}

//function call
The foreach (string s in GetFileNames (@ "E: \ log"))
{
TextBox1. Text +=s + "\ r \ n";
}
Used by CopyFile (@ "E: \ log \ error log", "@" E: \ log2 ");


Tests are available

CodePudding user response:

cited the 6th floor in the autumn red fruit response:
Quote: refer to the fifth floor ljg3081022a reply:

Quote: refer to the third floor of autumn red fruit response:
understand
People write good function, how to simplify the

Not written is my method named

The original is such, it can be
 
Private List GetFileNames (string filePath)
{
Return new DirectoryInfo (filePath.) GetFiles (). The Select (f=& gt; The cash-strapped ame). ToList ();
}

Private void used by CopyFile (string path1, string path2)
{
File. Copy (path1, path2 + "\ " + path1. Substring (path1. LastIndexOf (' \ \ ') + 1), false);
}

//function call
The foreach (string s in GetFileNames (@ "E: \ log"))
{
TextBox1. Text +=s + "\ r \ n";
}
Used by CopyFile (@ "E: \ log \ error log", "@" E: \ log2 ");


Tests are available


?????????? much

CodePudding user response:

Problem solving and to "post!
  •  Tags:  
  • C#
  • Related