1

I am developing an internal application which sends email to the users with the link to the training dcouments.

These documnets are placed in internal share drive, few of these documents have empty space in their names and thats causing the problem.

The path looks like \\Users\shared\Training\Database\Oracle\Docs\Oracle Database Admin.docx and i tried to replace empty space with %20 but still it doesn't work.. In the email link the path is trimmed to \\Users\shared\Training\Database\Oracle\Docs\Oracle

Public string GetMediaPath(int itemCode)
{
 string path = _dbContext.TraningMedias.Where( s => s.ItemCode == itemCode).Select(a => a.Path).FirstOrDefault().ToString();
 path.replace(" ", "%20");
 return path;
}

I dont understand why the replace function is not working in this case.

ajit
  • 31
  • 4
  • 1
    You'll probably need to directly set the link in the email body HTML. What's happening is that your email client is changing the hyperlink. How are the emails being sent? – WSC Jul 09 '20 at 09:16
  • sending mail via SMTP , this link is being sent in the email body its self. – ajit Jul 09 '20 at 09:45

6 Answers6

3

Strings are immutable, and Replace returns a string, so try this:

path = path.Replace(" ", "%20");
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1

To preserve the spaces in your link text, use an opening and closing chevron

Public string GetMediaPath(int itemCode)
{
 string path = "<"+ _dbContext.TraningMedias.Where( s => s.ItemCode == itemCode).Select(a => a.Path).FirstOrDefault().ToString() + ">";

 return path;
}
Alaa Mohammed
  • 382
  • 2
  • 14
0

Try doing this:

The below code will remove all invalid filename characters from the path.

 path =string.Concat(path.Split(Path.GetInvalidFileNameChars()));

Dont forget to include System.IO namespace.

Thanks

error_handler
  • 1,191
  • 11
  • 19
  • This is removing all the // in the path and making the entire path as a single string which is making it invalid path – ajit Jul 09 '20 at 09:44
0

You can try url encode adn get rid off spaces and others speacial characters.

path= HttpUtility.UrlDecode(path);
Sadullah DOĞAN
  • 335
  • 2
  • 10
0

Just convert the raw file path string to a proper URI, like this:

string fileUrl = new System.Uri("c:\\foo\\my document.docx").AbsoluteUri

which will give you this string: "file:///c:/foo/my%20document.docx"

Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42
0

Look this

In your case:

path = Uri.EscapeUriString(path);