要asp.net上传文件用,为每个用户组建立一个文件夹,用C#如何判断这个目录是否存在,如果不存在就创建

解决方案 »

  1.   

    Exists 方法
    .NET Framework 类库
    Directory..::.Exists 
    方法 确定给定路径是否引用磁盘上的现有目录。
    命名空间:  System.IO
    程序集:  mscorlib(在 mscorlib.dll 中) 示例// For File.Exists, Directory.Exists
    using System;
    using System.IO;
    using System.Collections;public class RecursiveFileProcessor 
    {
        public static void Main(string[] args) 
        {
            foreach(string path in args) 
            {
                if(File.Exists(path)) 
                {
                    // This path is a file
                    ProcessFile(path); 
                }               
                else if(Directory.Exists(path)) 
                {
                    // This path is a directory
                    ProcessDirectory(path);
                }
                else 
                {
                    Console.WriteLine("{0} is not a valid file or directory.", path);
                }        
            }        
        }
        // Process all files in the directory passed in, recurse on any directories 
        // that are found, and process the files they contain.
        public static void ProcessDirectory(string targetDirectory) 
        {
            // Process the list of files found in the directory.
            string [] fileEntries = Directory.GetFiles(targetDirectory);
            foreach(string fileName in fileEntries)
                ProcessFile(fileName);        // Recurse into subdirectories of this directory.
            string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
            foreach(string subdirectory in subdirectoryEntries)
                ProcessDirectory(subdirectory);
        }    // Insert logic for processing found files here.
        public static void ProcessFile(string path) 
        {
            Console.WriteLine("Processed file '{0}'.", path);        
        }
    }
      

  2.   

    补充一下
    原文地址:http://technet.microsoft.com/zh-cn/sysinternals/system.io.directory.exists.aspx 经常上去看看微软挺好 经常都会给一些示例代码
      

  3.   

    Exists 确定给定路径是否引用磁盘上的现有目录。             if (Directory.Exists(path)) 
                {
                    Console.WriteLine("That path exists already.");
                    return;
                }            // Try to create the directory.
                DirectoryInfo di = Directory.CreateDirectory(path);
                Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));            // Delete the directory.
                di.Delete();
                Console.WriteLine("The directory was deleted successfully.");
      

  4.   

    protected void btnUpload_Click(object sender, EventArgs e) 
        { 
            if (fileUpload.HasFile) 
            { 
                string savePath = Server.MapPath("~/upload/"); 
                if (!System.IO.Directory.Exists(savePath)) 
                {  
                    System.IO.Directory.CreateDirectory(savePath); 
                } 
                savePath = savePath + "\\" + fileUpload.FileName; 
                fileUpload.SaveAs(savePath);//保存文件  
            } 
        } 
      

  5.   

        try
                {
                   // string errorDirectory = @"D:\Program Files\error_salary";  //如果该目录不存在将创建
                    string errorPath = @"D:\Program Files\error_salary"  + "\\" + fileName +".txt";
                                    //start access file 
                    if (!System.IO.File.Exists(errorPath))
                    {
                        // Create a file to write to.
                        using (System.IO.StreamWriter sw = System.IO.File.CreateText(errorPath))
                        {
                            sw.WriteLine( exceptionString ); //将导常写入文件.
                        }
                    }
                    else
                    {
                        // This text is always added, making the file longer over time
                        // if it is not deleted.
                        using (System.IO.StreamWriter sw2 = System.IO.File.AppendText(errorPath))
                        {
                            sw2.WriteLine();
                            // sw.WriteLine(sqlTable);
                            sw2.WriteLine(DateTime.Now + "   ");
                            sw2.WriteLine( exceptionString );
                        }
                    }
                    //end            }
                catch (Exception ec)
                {
                    Console.WriteLine(ec.ToString());
                }
      

  6.   

        try
                {
                   // string errorDirectory = @"D:\Program Files\error_salary";  //如果该目录不存在将创建
                    string errorPath = @"D:\Program Files\error_salary"  + "\\" + fileName +".txt";
                                    //start access file 
                    if (!System.IO.File.Exists(errorPath))
                    {
                        // Create a file to write to.
                        using (System.IO.StreamWriter sw = System.IO.File.CreateText(errorPath))
                        {
                            sw.WriteLine( exceptionString ); //将导常写入文件.
                        }
                    }
                    else
                    {
                        // This text is always added, making the file longer over time
                        // if it is not deleted.
                        using (System.IO.StreamWriter sw2 = System.IO.File.AppendText(errorPath))
                        {
                            sw2.WriteLine();
                            // sw.WriteLine(sqlTable);
                            sw2.WriteLine(DateTime.Now + "   ");
                            sw2.WriteLine( exceptionString );
                        }
                    }
                    //end            }
                catch (Exception ec)
                {
                    Console.WriteLine(ec.ToString());
                }
      

  7.   

    楼上所有的都回答的是正确的System.IO.Directory.Exists("路径名");就这个