如何判断一个路径是否存在,不存在则创建

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

10 个解决方案

#1


Directory.Exists("路径名");

#2


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);        
    }
}

#3


补充一下
原文地址:http://technet.microsoft.com/zh-cn/sysinternals/system.io.directory.exists.aspx

 经常上去看看微软挺好 经常都会给一些示例代码

#4


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.");

#5


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);//保存文件  
        } 
    } 

#6


引用 1 楼 asdf311 的回复:
Directory.Exists("路径名");

一般用这个方法了。

#7


    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());
            }

#8




    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());
            }

#9


引用 6 楼 meceky 的回复:
引用 1 楼 asdf311 的回复:

Directory.Exists("路径名");

一般用这个方法了。


就是么。
这个足够啦。

#10


楼上所有的都回答的是正确的

System.IO.Directory.Exists("路径名");

就这个

#1


Directory.Exists("路径名");

#2


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);        
    }
}

#3


补充一下
原文地址:http://technet.microsoft.com/zh-cn/sysinternals/system.io.directory.exists.aspx

 经常上去看看微软挺好 经常都会给一些示例代码

#4


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.");

#5


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);//保存文件  
        } 
    } 

#6


引用 1 楼 asdf311 的回复:
Directory.Exists("路径名");

一般用这个方法了。

#7


    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());
            }

#8




    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());
            }

#9


引用 6 楼 meceky 的回复:
引用 1 楼 asdf311 的回复:

Directory.Exists("路径名");

一般用这个方法了。


就是么。
这个足够啦。

#10


楼上所有的都回答的是正确的

System.IO.Directory.Exists("路径名");

就这个