ASP.NET - how to get file from folder..? - Asked By sunil pandey on 09-Feb-12 02:31 AM

Hi all

i uploaded file into a folder.
and for mail attachment.
i want that file
button()
{
 string fname2 = FileUpload1.PostedFile.FileName;
   string SaveLocation = Server.MapPath("~/Quotation")+"\\"+fname2;
    
   EnhancedMailMessage msg = new EnhancedMailMessage();
 Attachment attach1 = new Attachment(SaveLocation);
 
  msg.Attachments.Add(attach1);
}
button()
{
 if (FileUpload1.HasFile)
      {
        string fname = FileUpload1.FileName.ToString();
    
        String fileName = fname;
        String SaveLocation = Server.MapPath("~/Quotation")+"\\"+fileName;

        FileUpload1.PostedFile.SaveAs(SaveLocation);
      }
}
    
dipa ahuja replied to sunil pandey on 09-Feb-12 02:44 AM
using System.Net.Mail;
using System.Net;
 
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress="xx@gmail.com";
  string GmailId="abc@gmail.com";
  string password="";
  string bodyMsg="helo its testing mail";
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = txtSubject.Text;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
  //Attach file using FileUpload Control and put the file in memory stream
 
  if (FileUpload1.HasFile)
  {
    mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
  }
 
  SmtpClient smtp = new SmtpClient("smtp.gmail.com");
  smtp.EnableSsl = true;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
  smtp.Send(mail);
}
 
smr replied to sunil pandey on 09-Feb-12 02:54 AM
hi

try this

public static void ProcessDir(string sourceDir, int recursionLvl)
{
  if (recursionLvl<=HowDeepToScan)
  {
  // Process the list of files found in the directory.
  string [] fileEntries = Directory.GetFiles(sourceDir);
  foreach(string fileName in fileEntries)
  {
     // do something with fileName
     Console.WriteLine(fileName);
  }
 
 
  // Recurse into subdirectories of this directory.
  string [] subdirEntries = Directory.GetDirectories(sourceDir);
  foreach(string subdir in subdirEntries)
     // Do not iterate through reparse points
     if ((File.GetAttributes(subdir) &
      FileAttributes.ReparsePoint) !=
        FileAttributes.ReparsePoint)
 
      ProcessDir(subdir,recursionLvl+1);
  }
}


follow
http://weblogs.asp.net/israelio/archive/2004/06/23/162913.aspx
http://bytes.com/topic/asp-net/answers/328297-getting-list-files-folder 
smr replied to sunil pandey on 09-Feb-12 02:55 AM
you can try this

public void Page_Load()
{
  System.IO.Directory StoreFile = null;
  string[] Files = null;
  string File = null;
  Files = StoreFile.GetFiles("drive:\\directory\\path\\", "*");
  foreach (string File_loopVariable in Files) {
    File = File_loopVariable;
    Response.Write(File + "<br>");
  }
}
Web Star replied to sunil pandey on 09-Feb-12 03:20 AM
if you want to upload one file and than attached that file into email attachement than simply first of all put code for saveas file from file control and than attched that file with mail object's attachment only need to set proper path of the file other thing done by that object itself
 if (FileUpload1.HasFile)
    {
      string fname = FileUpload1.FileName.ToString();
    
      String fileName = fname;
      String SaveLocation = Server.MapPath("~/Quotation")+"\\"+fileName;

      FileUpload1.PostedFile.SaveAs(SaveLocation);
    } 

//after than you need to send email and specify the correct path of that file
Attachment attachFile = new Attachment(SaveLocation );
MyMessage.Attachments.Add(attachFile);
Somesh Yadav replied to sunil pandey on 09-Feb-12 03:36 AM

Hi,

Here is another way to get filenames from all files, including subdirectories (notice the SearchOption.AllDirectories parametre):

// Process the list of files found in all directory.

string [] fileEntries = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories);

foreach(string fileName in fileEntries)

{

  // do something with fileName

  Console.WriteLine(fileName);

}

If you change the "*" to ex. "*.log" you will get all filenames that ends with ".log".