C# 操作文件夹及文件

Posted on 2008-11-14 15:41  sky-v  阅读(1721)  评论(1编辑  收藏  举报
class Program
    
{
       

        
/// <summary>
        
/// 追加文件,文件不存在则创建,存在则向文件写数据
        
/// </summary>
        
/// <param name="users"></param>
        
/// <param name="content"></param>

        public static void WriteFile(string users,string content)
        
{
            
try
            
{
                
string path = @"测试.log";
                
string mDate = DateTime.Now.ToString();
                
if (!File.Exists(path))//判断是否存在此文件
                {
                    StreamWriter sw 
= File.CreateText(path);
                    sw.WriteLine(users
+"\t"+mDate+"\t"+content);
                    sw.Close();
                }

                
else
                
{
                    StreamWriter sw 
= File.AppendText(path);
                    sw.WriteLine(users
+"\t"+mDate+"\t"+content);
                    sw.Close();
                }

            }

            
catch(Exception ee)
            
{
                
throw new Exception(ee.Message);
            }

        }



        
/// <summary>
        
/// 创建文件
        
/// </summary>

        public static void CreateFile()
        
{
            
string path = "测试1.log";
            
try
            
{
                
if (!File.Exists(path))//判断此文件是否存在,不存在则创建此文件
                {
                    FileStream fs 
= File.Create(path);
                    
//string str = "测试";
                    
//Byte[] info = Encoding.UTF8.GetBytes(str);
                    Byte[] info = 000000 };
                    fs.Write(info, 
0, info.Length);
                    fs.Close();
                }

            }

            
catch(Exception ee)
            

            }

        }



        
/// <summary>
        
/// copy文件
        
/// 支持copy后重新命名
        
/// </summary>

        public static void CopyFile()
        
{
            
string sourcePath = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\测试.log";//源文件
            string objectPath = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\测试1.log";//目标文件
            try
            
{
                
if(!File.Exists(sourcePath))//源文件
                {
                    
return;
                }

                
if(File.Exists(objectPath))//目标文件
                {
                    
return;
                }

                File.Copy(sourcePath,objectPath);
//这里的参数是两个文件路径,目标文件名可以不同于源文件名,效果是把源文件copy到新的路径,并且重新命名
            }

            
catch(Exception ee)
            
{ }
        }


        
/// <summary>
        
/// 移动文件
        
/// 支持移动后重新命名
        
/// </summary>

        public static void MoveFile()
        
{
            
string sourcePath = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\测试1.log";
            
string objectPath = @"C:\Documents and Settings\5.txt";//@"C:\Documents and Settings\测试.log";
            try
            
{
                
if(!File.Exists(sourcePath))//源文件
                {
                    
return;
                }

                
if(File.Exists(objectPath))//目标文件
                {
                    
return;
                }

                File.Move(sourcePath,objectPath);
//这里的参数是两个文件路径,目标文件名可以不同于源文件名,效果是把源文件移动到新的路径,并且重新命名
            }

            
catch(Exception ee)
            
{}
        }



        
/// <summary>
        
/// 删除文件
        
/// </summary>

        public static void DeleteFile()
        
{
            
string path = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\测试1.log";
            
try
            
{
                
if(!File.Exists(path))
                
{
                    
return;
                }

                File.Delete(path);
            }

            
catch(Exception ee)
            
{}
        }



        
/// <summary>
        
/// 获取文件信息
        
/// </summary>

        static string fileName = "";
        
static string fileLength = "";
        
static string fileTime = "";
        
static string fileAttributes = "";
        
public static void FileInfo()
        
{
            
string path = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\测试1.log";
            FileInfo fi 
= new FileInfo(path);
            
try
            
{
                
if (!File.Exists(path))
                
{
                    
return;
                }

                 fileName 
= fi.DirectoryName;//文件完整路径
                 fileLength = fi.Length.ToString();//文件长度
                 fileTime = fi.CreationTime.ToString();//文件创建的时间
                 fileAttributes = fi.Attributes.ToString();//属性,指得是右击文件-->属性-->常规下的高级-->存档和编制索引属性下的 可以存档文件选项
                 
//还有很多属性,用法差不多
            }

            
catch(Exception ee)
            
{}
        }



        
//以下是操作文件夹**************************************


        
/// <summary>
        
/// 追加文件夹,不存在则创建
        
/// </summary>
        
/// <param name="path"></param>
        
/// <returns></returns>

        public static string CreateDirectory1(string path)
        
{

            
try
            
{
                
if (Directory.Exists(path))
                
{
                    
return "已经有这个路径";
                }

                DirectoryInfo DirInfo 
= Directory.CreateDirectory(path);//用于创建指定目录的文件夹

                
return "路径创建成功!";
            }

            
catch (Exception ee)
            
{
                Console.WriteLine(ee.ToString());
                
return "N";
            }

        }




        
/// <summary>
        
/// 移动文件夹
        
/// 支持重新命名
        
/// </summary>

        public static void MoveDirectory()
        
{
            
string sourcePath = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\f";
            
string objectPath = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file";
            
try
            
{
                
if (!Directory.Exists(sourcePath))
                
{
                    
return;
                }

                
if (Directory.Exists(objectPath))
                
{
                    
return;
                }

                Directory.Move(sourcePath,objectPath);
            }

            
catch(Exception ee)
            
{}
        }



        
/// <summary>
        
/// 删除文件夹
        
/// </summary>

        public static void DeleteDirectory()
        
{
            
string path = @"C:\Documents and Settings\Administrator\桌面\flash\专项\操作文件夹\FoundFile\FoundFile\bin\Debug\file\新建文件夹";
            
try
            
{
                
if (!Directory.Exists(path))
                
{
                    
return;
                }

                Directory.Delete(path);
            }

            
catch(Exception ee)
            
{}
        }


         

        
static void Main(string[] args)
        
{
           

            
//追加文件
            
//WriteFile("zsp","测试此函数");


            
//创建文件
            
//CreateFile();

            
//copy文件
            
//CopyFile();


            
//移动文件
            
//MoveFile();


            
//删除文件
            
//DeleteFile();
            

            
///获取文件信息
            //FileInfo();
            
//Console.WriteLine(fileName);
            
//Console.WriteLine(fileLength);
            
//Console.WriteLine(fileTime);
            
//Console.WriteLine(fileAttributes);


            
//以下是操作文件夹
            
//追加文件夹
            
//string  path = Console.ReadLine();//这里是创建的文件夹路径,如果没有指定在哪个盘符下,那么默认为应用程序相同的路径下。
            
// Console.WriteLine(CreateDirectory1(path).ToString());


            
//移动文件夹
            
//MoveDirectory();


            
//删除文件夹
            
//DeleteDirectory();
        }

    }

Copyright © 2024 sky-v
Powered by .NET 8.0 on Kubernetes