Jigar patel
Jigar patel

Reputation: 215

how to replace image source of an html file using vb.net

how to replace image source of an html file with an folder name in the given path of an address bar in visual studio 2010

please anyone can help me with required logic will be really appreciated

Upvotes: 3

Views: 1428

Answers (2)

Jigar patel
Jigar patel

Reputation: 215

File.WriteAllText(TextBox1.Text & "\article.html", 
     File.ReadAllText(TextBox1.Text & "\article.html")
         .Replace("src=""img", " src=""/ielx5/" & last & "/img")
)

I did this and finally suceeded. Thank you for your help.

Upvotes: 0

Erdogan Kurtur
Erdogan Kurtur

Reputation: 3685

You can use two different paths

1- String.Replace to remove directory, but it may remove more than what you need.

var sourceDir = txtPath.Text; // \\192.168.0.2\kpo\Employee-Backup\Jigar\6373889-6388505_6388663
var html = File.ReadAllText("MyTest.html");

var cleanedHtml = string.Replace(html, source, "")

2- Use HtmlAgility library to load html, find IMG tags and replace SRC attribute of every IMG using String.Replace. Harder but safer.

update:

var separators = new char[] {
  Path.DirectorySeparatorChar,  
  Path.AltDirectorySeparatorChar  
};

var pathParts = sourceDir.Split(separators);

var root = string.Join("\\", pathParts.Take(pathParts.Length - 4));
var last4 = string.Join("\\", pathParts.Skip(pathParts.Length - 4));

Upvotes: 1

Related Questions