내 응용 프로그램에서 FileUploader 컨트롤을 사용합니다. 지정된 폴더에 파일을 저장하고 싶습니다. 이제이 폴더가없는 경우 먼저 폴더를 만든 다음 파일을이 폴더에 저장하고 싶습니다. 폴더가 이미 존재하면 파일을 저장하십시오.
어떻게해야합니까?
답변
다른 사람들이 말했듯이 사용 System.IO.Directory.CreateDirectory
그러나 먼저 존재하는지 확인할 필요는 없습니다. 로부터 문서
이미 존재하거나 경로의 일부가 유효하지 않은 한 경로에 지정된 모든 디렉토리가 작성됩니다. 디렉토리가 이미 존재하는 경우이 메소드는 새 디렉토리를 작성하지 않지만 기존 디렉토리에 대한 DirectoryInfo 오브젝트를 리턴합니다.
답변
http://forums.asp.net/p/1226236/2209871.aspx에 따라 아래 코드를 사용 하십시오 .
string subPath ="ImagesPath"; // your code goes here
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
답변
이 줄을 작성하십시오.
System.IO.Directory.CreateDirectory("my folder");
- 폴더가없는 경우 아직 존재하지 , 그것은됩니다 생성 .
- 폴더 가 이미 존재 하면 해당 줄은 무시 됩니다.
참조 : MSDN의 Directory.CreateDirectory에 대한 기사
물론 using System.IO;
소스 파일의 맨 위에 쓰고 Directory.CreateDirectory("my folder");
폴더를 만들 때마다 쓸 수도 있습니다.
답변
Directory.CreateDirectory
FilePath가 존재하지 않는 경우이를 작성하고 작성하는 방법을 설명합니다.
Directory.Exists
FilePath가 존재하는지 확인하는 방법을 설명합니다. 그러나 CreateDirectory가 확인하므로이 기능은 필요하지 않습니다.
답변
경로가 아직 없으면 다음과 같은 방법으로 경로를 만들 수 있습니다.
using System.IO;
private void CreateIfMissing(string path)
{
bool folderExists = Directory.Exists(Server.MapPath(path));
if (!folderExists)
Directory.CreateDirectory(Server.MapPath(path));
}
답변
이 메소드는 존재하지 않는 경우 폴더를 작성하고 존재하는 경우 아무 것도 수행하지 않습니다.
Directory.CreateDirectory(path);
답변
try / catch 절을 사용하여 존재하는지 확인하십시오.
try
{
if (!Directory.Exists(path))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
