如何根据用户ID创建文件夹

时间:2013-12-02 05:19:00

标签: c# asp.net asmx

我正在编写1个用于接收其他网站用户发送的PDF文件的web方法....这里是代码

<WebMethod()> _
Public Sub GetFile(ByVal fileBytes As Byte(), ByVal fileName As String, ByVal USER_ID   As Integer)
    System.IO.File.WriteAllBytes(Path.Combine(Server.MapPath("~"), fileName), fileBytes)
End Sub

结束班

上面的代码会将文件写入具有给定文件名的应用程序根文件夹...

但是在根文件夹中我想通过该用户标识的名称创建文件夹并将pdf文件存储在该文件夹中。

那么如何实现这个呢?

    System.IO.Directory.CreateDirectory(Server.MapPath("~/USER_ID/"))
    File.WriteAllBytes(Server.MapPath("~/USER_ID/"), fileBytes)

vaidate代码

2 个答案:

答案 0 :(得分:0)

如果您想根据用户ID,部门等创建文件夹结构,请尝试如下

Dim dinfo As New System.IO.DirectoryInfo(Server.MapPath(String.Format("~/{0}/{1}/", USER_ID, department)))

If Not dinfo.Exists Then
    dinfo.Create()
End If

System.IO.File.WriteAllBytes(System.IO.Path.Combine(dinfo.FullName, fileName), fileBytes)

答案 1 :(得分:0)

在向其添加文件之前,您应该确定该文件夹是否存在:

VB.net:

Imports System.IO


Dim path__1 As String = String.Format("~/{0}/", USER_ID)
Dim folder As String= Server.MapPath(path__1)
If Not Directory.Exists(folder) Then
    Directory.CreateDirectory(folder)
End If
System.IO.File.WriteAllBytes(Path.Combine(Server.MapPath(path__1), fileName), fileBytes)

C#:

using System.IO;


string path = String.Format("~/{0}/", USER_ID);

string folder = Server.MapPath(path);
if (!Directory.Exists(folder))
{
     Directory.CreateDirectory(folder);
}

System.IO.File.WriteAllBytes(Path.Combine(Server.MapPath(path), fileName), fileBytes);