Solved

Replace Server.MapPath with any path of windows?!

Posted on 2012-04-08
6
436 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 74

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

7 Experts available now in Live!

Get 1:1 Help Now