Solved

Replace Server.MapPath with any path of windows?!

Posted on 2012-04-08
6
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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