Solved

Replace Server.MapPath with any path of windows?!

Posted on 2012-04-08
6
441 Views
Last Modified: 2012-04-09
Hello,

I have below code to send email with attachment. Code is working, but only if file is in same root as project files.
But, I need that for example I can choose test.jpg from any path, like D:\test.jpg

Any idea?

Dim NewAttachment As Attachment = New Attachment(Server.MapPath("test.jpg"))
Mail.Attachments.Add(NewAttachment)

Open in new window


Thanks in advance for help.
0
Comment
Question by:dejandejanovic
  • 2
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 37822946
Try:

Dim NewAttachment As Attachment
Dim filename As String = "test.jpg"

If System.IO.Path.IsPathRooted(filename) Then
    NewAttachment = New Attachment(filename)
Else
    NewAttachment = New Attachment(Server.MapPath(filename))
End If

Mail.Attachments.Add(NewAttachment)

Open in new window

0
 

Author Comment

by:dejandejanovic
ID: 37823913
Hi,
thank you for help. Code is working.
Now, I have try to replace string "test.jpg" with FileUpload control. I was positive that code will continue working, but get an error.
Dim filename As String = FileUpload1.FileName
        System.IO.Path.IsPathRooted(filename)
        Dim NewAttachment As Attachment = New Attachment(filename)
        Mail.Attachments.Add(NewAttachment)

Open in new window


ERROR:
Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\test.jpg'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\test.jpg'.

Thanks...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37824458
You haven't included all of the code I posted. The function System.IO.Path.IsPathRooted returns a boolean value indicating whether or not the path being passed to it starts with a directory marker (a slash) or a drive and colon. If the return is false, then you are dealing with a relative path. I used this return value in as a part of an If/Else statement so that if you were dealing with a relative path, then Server.MapPath would be called; otherwise, the full, unaltered path would be passed to your Attachment constructor (since it's an absolute path at that point).

Please review the logic I posted above.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 100 total points
ID: 37824682
With FileUpload control, you would need to save the file to disk first and then use that path in attachment.
0
 

Author Closing Comment

by:dejandejanovic
ID: 37825145
Got it.
Thank you for help, tips, and support.
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) 
        Dim NewAttachment As New Attachment(FileUpload1.FileContent, fileName) 
        Mail.Attachments.Add(NewAttachment)

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now