i have stucked on bellow problem in WPF C#.
Variable 'path' gets deserialized from xml file band contains following:
string path="D:\\test.mp4"
or
string path=#"D:\test.mp4"
uri i = new uri(path)
somehow path does not get recognized. Length is 12 instead of 11, i think because "\" does not get recongnized as path seperator. I have tried to sub string it and add it like this
string ss="D:" + #"\" + "test.mp4"
uri i = new uri(ss)
and still does not work. I tried with Path.combine also
Any idea?
I see you want to create a URI object and D:/test.mp4 is not a URI string. hence, try this to see if it works:
string path="file://D:/test.mp4"
uri i = new uri(path)
Related
Hi am facing issue to pass path in File.Copy() method.
Here I have created a string dest. While I am passing it in File.copy(), it is taking "\" double slash. Because of this, I am getting error of illegal character. Please look into it.
string dest = (#"\" + Environment.MachineName +#"\"+ Path.Replace(#"\\",#"\")).Replace(":", "$"); //the value get -"pt-LTP-109\\C$\\Temp\\192.168.0.205\\fileFolder"
dest = dest.Replace("\\\\", #"\") +"\\"+ "filename.txt"; // the value get -"\\pt-LTP-109\\C$\\Temp\\192.168.0.205\\fileFolder\\filename.txt"
dest = ("\"").ToString()+dest+"\""; //the value get- "\"\\pt-LTP-109\\C$\\Temp\\192.168.0.205\\fileFolder\\filename.txt\""
File.Copy(source, dest, true);`
That is a very complicated way of doing something so simple... To convert a normal path into a UNC path you only need to do two things:
Replace : with $ (which you are doing correctly).
Prepend the path with two backslashes and the machine name.
Your code can be shortened to this:
string dest = System.IO.Path.Combine(#"\\" + Environment.MachineName, Path.Replace(":", "$"), "filename.txt");
Try
Path.GetFullPath(dest).Replace(#"\",#"\\");
I have a file test.html on a relative path in c#:
string path1 = "/sites/site/folder/subfolder1/subfolder2/subfolder3/test.html";
Inside the file test.html I have a link to resource:
string path2 = "../../../subfolder4/image.jpg";
I want to calculate complete relative path to this resource against the same relative root as represented in path1 to get the following path3:
string path3 = CalculateRelativePath(path1, path2);
Assert.AreEqual(path3, "/sites/site/folder/subfolder4/image.jpg");
Are there any standard functions to do this? Thank you.
You can use this:
var page = new Uri(new Uri("http://dont-care"), path1);
var path3 = new Uri(page, path2).LocalPath;
I don't think there is a standard function to do this. You could write your own function, splitting both string paths to an array (using "/"), then using the array parts which equal ".." from one, to navigate up the other array.
How to convert
"String path = #"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
into
String path = #"C:\Abc\Omg\Why\Me\".
My approach is to first reverse the string and then remove all the "\" till we get first char, and the reverse it again.
How to do this in C#, is there any method for such operation?
You can just construct path using the Path static class:
string path = Path.GetFullPath(#"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\");
After this operation, variable path will contain the minimal version:
C:\Abc\Omg\Why\Me\
You can use path.TrimEnd('\\'). Have a look at the documentation for String.TrimEnd.
If you want the trailing slash, you can add it back easily.
var path = #"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
path = path.TrimEnd('\\') + '\\';
another solution is
var path = #"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
path = Path.GetFullPath(path);
I am passing a query string parameter containing file name.
default.aspx?file=Fame+ adlabs.xml (Fame+ adlabs.xml is the actual file name on server). The file name has "+" & also blank spaces.
When I check for file name from query string as follows:
var fileName = Request.QueryString["file"];
The variable filename does not have a "+" in it. It reads as "Fame adlabs.xml" & hence I get a file not found exception. I cannot rename the xml files. Can someone please guide me into right direction.
Thanks
If you are trying to do it at the server in C#:
String FileName = "default.aspx?";
String FullURL = FileName + HttpUtility.UrlEncode("Fame + adlabs.xml");
String Decoded = HttpUtility.UrlDecode(FullURL);
You should URL encode into your javascript before sending it :
var name = "Fame+ adlabs.xml";
var url = "default.aspx?file=" + encodeURIComponent(name);
Pay attention that following char won't work : ~!*()'
I have a textbox where users can paste a URL address. I want to add a directory name to the URL before saving it in the database.
<asp:TextBox ID="urlTextbox" runat="server"></asp:TextBox>
Code behind
TextBox url = urlTextbox as TextBox;
string urlString = urlTextbox.Text;
Let's say the urlString = "mydomain.com/123456". I want to replace it with "mydomain.com/directory/123456". mydomain.com/directory is the same for all the URLs. The last part "123456" changes only.
Thank you
I'd suggest seeing if your needs are met with the UriBuilder class.
UriBuilder url = new UriBuilder(urlTextbox.Text);
Now you can use the various properties to change your url.
string formattedUrl = string.Format("{0}://{1}/directory/{2}", url.Scheme, url.Host, url.Path);
A better idea is to adjust the URL with another / same UriBuilder as noted by Jared.
UriBuilder url = new UriBuilder(urlTextbox.Text);
url.Path = string.Format("directory/{0}", url.Path);
Use this object as a Uri by simply doing this
Uri formattedUrl = url.Uri;
Or convert to a string if needed.
string formattedUrl = url.ToString();
You can also use Uri.TryParse(...) to verify if it's a valid URL being entered into the text box.
To get the individual query parameters, you can look at the Uri object.
UriBuilder url = new UriBuilder("mydomain.com/123456?qs=aaa&bg=bbb&pg=ccc");
url.Path = string.Format("directory/{0}", url.Path);
Uri formattedUrl = url.Uri;
string queryString = formattedUrl.Query;
// parse the query into a dictionary
var parameters = HttpUtility.ParseQueryString(queryString);
// get your parameters
string qs = parameters.Get("qs");
string bg = parameters.Get("bg");
string pg = parameters.Get("pg");
You can use string functions Split and Join to achieve your result. An example code is shown below
List<string> parts = urlString.Split(new char[] { '/'}).ToList();
parts.Insert(parts.Count - 1, "directory");
urlString = string.Join("/", parts);
This is one way of doing. Split the urlString using .split() function.
string[] parts = urlString.Split('/');
parts[parts.Length-1] will have that number. Append it to the string you want.
I'd do something like this:
//Assuming the address in urlString has the format mydomain.com/123456
string[] urlParts = urlString.Split('/');
string directory = "directory";
string finalUrl = urlParts[0] + "/" + directory + "/" + urlParts[1];
Be careful if the address has other "/" characters, like if preceded by http:// or something like that.
Hope it helps.
Simply use concatenation:
save in a temporary string
temp="mydomain.com/directory/"
and save the changing part in another string like
temp2="123456"
now concatenate both temp1 and temp2 like below.
urlString=temp1+temp2;