利用Session变量解决勇在LetterFolderCustomFileNameGenerator.cs文件内的GenerateFileName()函数动态碟成如 "P00001" "P01009"等样式的目录

 public string GenerateFileName(UploadedFile file)

  {

   //string fileName = Path.Combine(file.ClientName.Substring(0, 1).ToLower(), file.ClientName);

   //DirectoryInfo di = Directory.CreateDirectory(@"d:cnawebjournal-center.comXYZ");

   //CreateDirectory(HttpContext.Current.Server.MapPath("../") + HttpContext.Current.Request.QueryString["PaperIDID"]);


   CreateDirectory(HttpContext.Current.Server.MapPath("../") + HttpContext.Current.Session["FolderName"]);

   string fileName = Path.Combine((string)HttpContext.Current.Session["FolderName"], file.ClientName);

   return fileName;

  }

在LetterFolderCustomFileNameGenerator.cs文件中直接使用Session["FolderName"]会发生错误,

在文章​​http://dotnetspider.com/Question696.aspx​​找到答案

Hi All

 i m new to asp.net, any help will be appriciated ,

i have a login form where i get login/pass if success i generate two sessions like

Session["EmailAddress"] = EmailAddress;

Session["Name"] = Name;

upto here its pretty ok .. i have a .cs class naming user.cs it has different methods like AdUser,updateUser etc


the problem is that when ever i try to get the vale of session in this class it gives me this error : The name 'Session' does not exist in the class or namespace 'RMS.User'

y the session varible is not visible here ? or what should i do to get its value


Waiting 4 reply,

Qazi Asim

use this in your class files to access the session:

HttpContext.Current.Session("EmailAddress")

Example:

public class user

{

   public void MySessionValues()

   {

       string myEmailAddress = HTTPContext.Current.Session["EmailAddress"];

   }

}


利用SlickUpload上传文件的问题


By using the CreateDirectory() function provided in the following artical

​http://www.codeproject.com/csharp/CreateDirectorymethod.asp​

now, we can create a new folder for uploading files.

in LetterFolderCustomFileNameGenerator.cs, we add the function public static void CreateDirectory(string DirectoryPath)

public class LetterFolderCustomFileNameGenerator : ICustomFileNameGenerator

 {

  public string GenerateFileName(UploadedFile file)

  {

   //string fileName = Path.Combine(file.ClientName.Substring(0, 1).ToLower(), file.ClientName);

   //DirectoryInfo di = Directory.CreateDirectory(@"d:\cna\web\journal-center.com\XYZ");

   CreateDirectory("d:\\cna\\web\\journal-center.com\\" + "XYZNow");

   string fileName = Path.Combine("XYZNow", file.ClientName);

   return fileName;

  }

  public static void CreateDirectory(string DirectoryPath)

  {

   // trim leading \ character

   DirectoryPath = DirectoryPath.TrimEnd(Path.DirectorySeparatorChar);

   Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();

   // check if folder exists, if yes - no work to do

   if(!fso.FolderExists(DirectoryPath))

   {

    int i = DirectoryPath.LastIndexOf(Path.DirectorySeparatorChar);

    // find last\lowest folder name

    string CurrentDirectoryName = DirectoryPath.Substring(i+1,

     DirectoryPath.Length-i-1);

    // find parent folder of the last folder

    string ParentDirectoryPath = DirectoryPath.Substring(0,i);

    // recursive calling of function to create all parent folders

    CreateDirectory(ParentDirectoryPath);

    // create last folder in current path

    Scripting.Folder folder = fso.GetFolder(ParentDirectoryPath);

    folder.SubFolders.Add(CurrentDirectoryName);

   }

  }