how to replace “\” with “_” in string in c#.net

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

2

Let’s see how to replace “\” with “_” in string in c#.net

Since it’s not straight forward by using String.Replace method.

So let’s take move ahead with it

suppose your string item is like that
string filepath=”C:\\\\DemoFolder\\Claims”;

now use below code

filepath.Replace(@”\”, @”_”);

Output=>  “C:__DemoFolder_Claims”

 

read all file names exists in perticular directory

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

2

 

Let’s see how to read all file names exists in perticular directory

 

dd1.png

 

string downloadpath = @”C:\\1″;
DirectoryInfo dir = new DirectoryInfo(downloadpath);
var files = dir.GetFiles(“*.*”);
foreach (var file in files)
{
FilesInfo obj = new FilesInfo();
obj.FileName = file.Name;
lstFileinfo.Add(obj);
}

How to check particular url is exist or not with the help of HttpWebResponse ?

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

2

 

Let’s see,

 

How to check particular url is exist or not with the help of HttpWebResponse 

 

[Route(“Check”)]
public HttpResponseMessage Check(string appUrl)
{

//Set as below
appUrl=https://sandippatil.com/Downloads/Training ManualHTML.pdf”;

Uri destURL = new Uri(appUrl);
HttpWebRequest request = HttpWebRequest.CreateHttp(destURL);
request.Method = “HEAD”;
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
helpEnabled = true;
}
}
catch (Exception ex)
{
helpEnabled = false;
}
HttpResponseMessage resp;
resp = Request.CreateResponse<bool>(HttpStatusCode.OK, helpEnabled);
return resp;
}

 

If doesn’t exists it will throws an exception as shown in below Image.

sk2

 

Another solution is

var appUrl=”https://sandippatil.com/Downloads/Training ManualHTML.pdf”;

bool urlExists;

WebRequest webRequest = WebRequest.Create(appUrl);
WebResponse webResponse;
try
{
webResponse = webRequest.GetResponse();
urlExists= true;
}
catch //If exception thrown then couldn’t get response from address
{
urlExists= false;
}
HttpResponseMessage resp;
resp = Request.CreateResponse<bool>(HttpStatusCode.OK, helpEnabled);
return resp;

 

Working code in WebAPI controller is

 

[Route(“CheckHelpButtonPermission”)]
public HttpResponseMessage CheckHelpButtonPermission(string appUrl)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(appUrl);
webRequest.Method = “GET”;

string responseData = string.Empty;
HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();

using (StreamReader responseReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseData = responseReader.ReadToEnd();
helpEnabled = true;
}
}
catch (System.Net.WebException ex)
{
helpEnabled = false;
}

HttpResponseMessage resp;
resp = Request.CreateResponse<bool>(HttpStatusCode.OK, helpEnabled);
return resp;
}