none
Saving e-Mail attachment not working with variable name RRS feed

  • Question

  • Hello,

    I am trying to save an email attachment to a dir. 

    I have this code working, but I am now trying to amend it so that it saves into the dir where the exe is being run from , but it seems to fail ( ie: the program cannot locate it).

    filename = Environment.ExpandEnvironmentVariables(@"file" + Code + ".xml")

    if (mail.Attachments.Count > 0) { for (int i = 1; i <= mail .Attachments.Count; i++) { mail.Attachments[i].SaveAsFile

    (Environment.ExpandEnvironmentVariables(filename)); } mail.UnRead = false; }

    fails.

    if (mail.Attachments.Count > 0)
     {
      for (int i = 1; i <= mail
       .Attachments.Count; i++)
       {
        mail.Attachments[i].SaveAsFile
    (@"C:\Users\me\Documents\Visual Studio 2017\Projects\proj2\proj2\bin\Debug\file" + Code + ".xml");
    }
    mail.UnRead = false;
    }

    works

    I have full access to the dir, so I am not sure why one would work and the other would not?

    could someone help me understand this, please?

    Thank you


    • Edited by G-Oker Thursday, April 19, 2018 1:07 PM
    Thursday, April 19, 2018 1:06 PM

Answers

  • The best way to save a file in a folder one level down from the project folder is to first check if the folder exists as follows then if so do your work. Note the use of Path.Combine and AppDomain.CurrentDomain.BaseDirectory.

    If not using C# 6 or higher replace $"{Code}.xml" with conventional string formatting or string concatenation. 

    var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file");
    if (Directory.Exists(folder))
    {
        var fileName = Path.Combine(folder, $"{Code}.xml");
        // do what you need to with saving
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, April 19, 2018 2:03 PM
    Moderator

All replies

  • The best way to save a file in a folder one level down from the project folder is to first check if the folder exists as follows then if so do your work. Note the use of Path.Combine and AppDomain.CurrentDomain.BaseDirectory.

    If not using C# 6 or higher replace $"{Code}.xml" with conventional string formatting or string concatenation. 

    var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file");
    if (Directory.Exists(folder))
    {
        var fileName = Path.Combine(folder, $"{Code}.xml");
        // do what you need to with saving
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, April 19, 2018 2:03 PM
    Moderator
  • Hi Karen,

    thank you.

    <g class="gr_ gr_116 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="116" id="116">Adding </g>

    var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
    <g class="gr_ gr_116 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" data-gr-id="116" id="116">and</g> changing my code to 
    if (mail.Attachments.Count > 0)
    {
     for (int i = 1; i <= mail
        .Attachments.Count; i++)
     {
         mail.Attachments[i].SaveAsFile
         (folder);
     }
      mail.UnRead = false;
    }
    worked. :-)

    Thursday, April 19, 2018 3:06 PM