URI formats are not supported in c# - c#

I am trying to insert my data in to xml file. the xml file location
path: http://AutoCompleteInGridView%20new1/Design/Pro/pricelist.xml.
when inserting my data i got error URI formats are not supported.
It shows argument exception was unhandled.I want to save my xml
file in server system.these url belongs to ServerPath location.
can anyone give me a suggestion or links to solve these problem.
here is the error:

Use this sample:
string uriPath = "YourAddress/pricelist.xml";
string localPath = new Uri(uriPath).LocalPath;

Try like this:
string SavePathUrl = ConfigurationManager.AppSettings["SavePathUrl"];
string strFileName = DateTime.Now.ToString("dd-mmm-yyyy-hh-mm-ss-ffffff") + "^" + fileName;
File.WriteAllBytes(new Uri(SavePathUrl).AbsoluteUri + strFileName, Convert.FromBase64String(base64String));
return strFileName;

you could try with this solution:
http://social.msdn.microsoft.com/Forums/en-US/eccd585a-ac2b-4700-aa28-abb4802cd3a5/uri-formats-are-not-supported-error-with-xmlsave?forum=xmlandnetfx
Basically, use
doc.Save(Server.MapPath("~/EEPPriceList/Products.xml"))

Related

How to get file name from a path in c#

I want to extract the filename from a file path in C#.
For example:
textBox1.Text = "C:\Users\Elias\Desktop\image.png"
I want to copy the file name: "image.png" to the textBox2
How can i do that?
Use the static Path.GetFileName method in System.IO:
Path.GetFileName(#"C:\Users\Elias\Desktop\image.png"); // --> image.png
regarding your example:
textBox2.Text = Path.GetFileName(textBox1.Text);
System.IO.FileInfo class can help with parsing that information:
textBox2.Text = new FileInfo(textBox1.Text).Name;
MSDN documentation on the FileInfo class:
https://msdn.microsoft.com/en-us/library/system.io.fileinfo(v=vs.110).aspx

Create a copy of a file in same location as original

The end-user supplies a path, indicating where the original document is.
string DocxFileName = "C:\\WorksArshad\\3.docx";
I'd like to create a copy of the document name 3.docx as 3Version1.docx and store the copy in the same directory as the original.
How do I get the whole path without the file name and extension?
(i.e.) I need to get the "C:\\WorksArshad\\" path alone.
FileInfo file = new FileInfo(Session.FileName);
string path = file.Directory.tostring();
and then using
string fileName = Path.GetFileNameWithoutExtension(Session.FileName);
string DocxFileNamee = path + "\\" + fileName + "V1.docx";
File.Copy(Session.FileName, DocxFileNamee, true);
where in Session.FileName = "C:\WorksArshad\3.docx" and in path I'd get "C:\WorksArshad"
requirement solved .
Or
File.Copy(Session.FileName, Path.Combine(Path.GetDirectoryName(Session.FileName)
, Path.GetFileNameWithoutExtension(Session.FileName)+"V1.docx"),true);
both the above gives the solution

"The given path's format is not supported" when trying to create file based on current time

Is there any way to save a file with a variable name?
For example, I have this code but I got error in the StreamWriter:
string hour = DateTime.Now.ToString("HH:mm:ss");
string date = DateTime.Now.ToShortDateString();
string filename = "Numbers " + date + " " + hours+ ".txt";
string path = #"C:\ShowMySms\" + filename;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true)) {
file.WriteLine("test");
}
I also tried use
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\ShowMySms\" + filename, true))
but both ways give me an error.
Is there any way for me to export files with a different using StreamWriter?
Thank you in advance!
Edit: the error is
The given path's format is not supported.
I tried do
Path.Combine(path, filename);
and put the path in the StreamWriter but still getting the same error...
Try using this formatting instead:
string filename = string.Format("Numbers_yyyyMMdd_HHmmss.txt", date);
This will give you file names like
Numbers_20131118_155410.txt
which contain no "dangerous" and illegal characters (like :) in your file name ...
you can also try below code snippet
string filename = "Numbers" + DateTime.Now.ToString("dd-MM-yyyy_HHmmss")+ ".txt";
Output:
Numbers06-02-2018_122446.txt

xmltextwriter path in c#

I am trying to use xmltextwriter and assign a path which needs to use for writing.
I am trying this:
string path = "~/Uploads/site/" + Current.User.Id + .kml";
XmlTextWriter xtr = new XmlTextWriter(path, System.Text.Encoding.UTF8);
I want the file to be saved in the uploads/site/ folder within the website directory, but I am getting an error:
Could not find a part of the path 'c:\windows\system32\inetsrv\~\Uploads\site\16.kml'.
I would like to know how I can assign the desired path to the xmltextwriter.
Thanks in advance, Laziale
Use server.MapPath method to get the right path.
string path = Server.MapPath("~/Uploads/site/" + Current.User.Id + ".kml");
Heres an error
string path = "~/Uploads/site/" + Current.User.Id + .kml";
should be
string path = "~/Uploads/site/" + Current.User.Id + ".kml";
Still it wont work, and the answer is illustrated in this question Map the physical file path in asp.net mvc
You get this error because you need to use Server.MapPath
Otherwise the code is trying to map on your pc and not the server
string path = Server.MapPath("~/Uploads/site/" + Current.User.Id + ".kml");

Change File Extension Using C#

I have many file types: pdf, tiff, jpeg, bmp. etc.
My question is how can I change file extension?
I tried this:
my file= c:/my documents/my images/cars/a.jpg;
string extension = Path.GetExtension(myffile);
myfile.replace(extension,".Jpeg");
No matter what type of file it is, the format I specify must be with the file name. But it does not work. I get file path from browser like c:\..\..\a.jpg, and the file format is a.jpeg. So, when I try to delete it, it gives me an error: Cannot find the file on specified path'. So, I am thinking it has something to do with the file extension that does not match. So, I am trying to convert .jpg to .jpeg and delete the file then.
There is: Path.ChangeExtension method. E.g.:
var result = Path.ChangeExtension(myffile, ".jpg");
In the case if you also want to physically change the extension, you could use File.Move method:
File.Move(myffile, Path.ChangeExtension(myffile, ".jpg"));
You should do a move of the file to rename it. In your example code you are only changing the string, not the file:
myfile= "c:/my documents/my images/cars/a.jpg";
string extension = Path.GetExtension(myffile);
myfile.replace(extension,".Jpeg");
you are only changing myfile (which is a string). To move the actual file, you should do
FileInfo f = new FileInfo(myfile);
f.MoveTo(Path.ChangeExtension(myfile, ".Jpeg"));
See FileInfo.MoveTo
try this.
filename = Path.ChangeExtension(".blah")
in you Case:
myfile= c:/my documents/my images/cars/a.jpg;
string extension = Path.GetExtension(myffile);
filename = Path.ChangeExtension(myfile,".blah")
You should look this post too:
http://msdn.microsoft.com/en-us/library/system.io.path.changeextension.aspx
The method GetFileNameWithoutExtension, as the name implies, does not return the extension on the file. In your case, it would only return "a". You want to append your ".Jpeg" to that result. However, at a different level, this seems strange, as image files have different metadata and cannot be converted so easily.
Convert file format to png
string newfilename ,
string filename = "~/Photo/" + lbl_ImgPath.Text.ToString();/*get filename from specific path where we store image*/
string newfilename = Path.ChangeExtension(filename, ".png");/*Convert file format from jpg to png*/
Alternative to using Path.ChangeExtension
string ChangeFileExtension(ReadOnlySpan<char> path, ReadOnlySpan<char> extension)
{
var lastPeriod = path.LastIndexOf('.');
return string.Concat(path[..lastPeriod], extension);
}
string myfile= #"C:/my documents/my images/cars/a.jpg";
string changedFileExtesion = ChangeFileExtension(myfile, ".jpeg");
Console.WriteLine(changedFileExtesion);
// output: C:/my documents/my images/cars/a.jpeg

Categories