string txt = textBox1.Text;
string filename = "temp.txt"; //文件名,可以带路径
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename);
sw.Write(txt);
sw.Close();
选择菜单栏的“文件” 再选择“另存为”即可
思路如下:
using System.IO;
public static string pathDir = string.Empty;
public static string savePath = string.Empty;
public static StringBuilder sb = new StringBuilder();
static void Main(string[] args)
{
pathDir = @"d:\WES 2009 相关资料\";
savePath = @"D:\filePath.txt";
GetDirectories(pathDir);
//FindFile(pathDir);
Console.ReadLine();
}
///
/// 获取本地制定位置文件夹及所有文件
///
/// 路径
private static void GetDirectories(string path)
{
try
{
string[] fileNames = System.IO.Directory.GetFiles(path);
string[] directories = System.IO.Directory.GetDirectories(path);
foreach (string dir in directories)
{
Console.WriteLine("Directoriesname:{0}", dir);
sb.Append(dir + "\\" + "\r\n");
GetDirectories(dir + "\\");
}
foreach (string file in fileNames)
{
Console.WriteLine("Filename:{0}", file);
sb.Append(file + "\r\n");
}
WriteText(savePath, sb.ToString());
Console.WriteLine("All directories and files have been written into filePath.txt successfully!");
}
catch (Exception)
{
throw;
}
}
///
/// 递归实现查找目录下的所有子目录和文件
///
/// 指定的目录
public static void FindFile(string dir)
{
//在指定目录及子目录下查找文件
DirectoryInfo Dir = new DirectoryInfo(dir);
try
{
foreach (DirectoryInfo d in Dir.GetDirectories()) //查找子目录
{
sb.Append(Dir + d.ToString() + "\\"+ "\r\n");
FindFile(Dir + d.ToString() + "\\");
}
foreach (FileInfo f in Dir.GetFiles("*.*")) //查找文件
{
sb.Append(Dir + f.ToString() + "\r\n");
}
WriteText(savePath, sb.ToString());
Console.WriteLine("All directories and files have been written into filePath.txt successfully!");
}
catch (Exception)
{
throw new Exception();
}
}
///
/// 写入文件
///
/// save path
/// data info
public static void WriteText(string savePath, string dataInfo)
{
try
{
if (!File.Exists(savePath))
{
System.IO.File.Create(savePath);
}
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter(savePath);
sw.WriteLine(dataInfo); //Write a line of text
sw.Close(); //Close the file
}
catch (Exception)
{
throw;
}
}
///
/// 取本地目录内的文件名
///
///
private void showDirFiles(string strPath)
{
try
{
if (!Directory.Exists(strPath))
{//目录不存在,创建目录
Directory.CreateDirectory(strPath);
}
DirectoryInfo mydir = new DirectoryInfo(strPath);
FileInfo[] files = mydir.GetFiles();
this.listBoxC.Items.Clear();//清楚listbox中现有项目
for (int i = 0; i < files.Length; i++)
{
this.listBoxC.Items.Add(files[i].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void ListFiles(FileSystemInfo info)
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
//是文件
if (file != null)
{
//Console.WriteLine(file.FullName + "\t " + file.Length);
if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg")
//此处为显示JPG格式,不加IF可遍历所有格式的文件
{
this.list1.Items.Add(file);
//MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
}
}
//对于子目录,进行递归调用
else
{
ListFiles(files[i]);
}
}
}
}
File.Copy