C# 如何基于用户ID创建文件夹

C# 如何基于用户ID创建文件夹,c#,asp.net,asmx,C#,Asp.net,Asmx,我正在写一个webmethod来接收其他网站用户发送的PDF文件…下面是代码 <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)

我正在写一个webmethod来接收其他网站用户发送的PDF文件…下面是代码

<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

如果要根据用户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)

在将文件添加到文件夹之前,应确保该文件夹存在:

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#:


damith..我希望用户拥有不同的用户ID,因此应创建不同的文件夹,然后根据用户ID将其各自的pdf文件保存在该文件夹中..我希望您不知道我想做什么..如21..has 1.pdf,,,,,23有s.pdf///like thhis.。如果我想添加更多的文件夹,如部门名称,那么???我在上面的评论中提到了一些疑问。因此Damith…回复Damith,seyed。我得到错误:部门是一种不能用作expression@loveforasp.net与发送
USER\u ID
参数相同,您也需要发送“department”,结合你的代码和Damith代码,什么是完整的代码?从技术上讲,代码是相等的。但是我的代码是安全的,因为它首先通过名称
用户ID
检查是否存在任何文件夹。如果是,请不要创建该文件夹,否则就创建它。我只是使用了damith代码。我得到错误:建议我使用您的代码,请参阅damith。我已提到部门以及用户ID…操作系统我要求您提供包含两者的代码。您没有提到什么是
部门
。请将其添加到您的问题中,我将帮助您。我也尝试了您的代码。获取未定义VAR的错误
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)
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);