?
Solved

Replace Server.MapPath with any path of windows?!

Posted on 2012-04-08
6
Medium Priority
?
494 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 1600 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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 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