xmltextwriter path in c# - 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");

Related

creating a textfile and cannot identify the location the created file goes

What is wrong with this code? The text file is not created in the bin\Debug directory!
string path = Environment.CurrentDirectory + "/" + "Contacts.txt";
if (!File.Exists(path))
{
File.CreateText(path);
MessageBox.Show("file created successfully");
}
using (StreamWriter sw = new StreamWriter("path", true))
{
sw.WriteLine(txtCntname1.Text + "," + txtCntnum1.Text + "," + txtCntnum2.Text + "," + txtCntnum3.Text + ",");
sw.Close();
}
You have two ways of calculating EXE's directory:
1) Implicit
You can use only the file name. In this case, the file will be created in the same folder with EXE file:
File.CreateText("Contacts.txt")
2) Explicit
If you need reliable way to get the EXE's directory, then you need to use AppDomain:
string exe_dir = AppDomain.CurrentDomain.BaseDirectory;
Then, instead of manually concatenating strings to form a path, use Path.Combine method:
string file_path = Path.Combine(exe_dir, "Contact.txt");
The code (Assembly.GetExecutingAssembly().Location), offered by #TobiasTengler, won't work, for instance, for Excel/Word add-in.
Aside from that path should probably not be in quotes here:
using (StreamWriter sw = new StreamWriter("path", true))
Do not use Environment.CurrentDirectory! It does not always represent the directory you started your executable from, since it only represents your working directory.
Please use Assembly.GetExecutingAssembly().Location to get the full path of the executing assembly and then extract only the directory from it, using:
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Alternatively you could also use AppDomain.CurrentDomain.BaseDirectory for identifying the directory your application is residing in.
Of course not using an absolute path is a possibility as well, you can create your file using a relative path, by not specifying any directory:
File.CreateText("Contacts.txt")
EDIT:
Seeing how you formed your path, please also use Path.Combine to build your paths:
var appBaseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var contactsFilePath = Path.Combine(appBaseDir, "Contacts.txt");

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

URI formats are not supported in 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"))

Retrive image file from folder and display into Image Control

I have save my Image using Below code In a Folder in my source file..
now I have to retrvive the same saved Image on my Form
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename;
fileupload1.SaveAs(Server.MapPath("Image/" + strtemp));
So how should i give a Path to my Image Control
I have tried something like this to get a path of file and folder but simply i cant fetch the image from it into my image folder
Image2.ImageUrl = (Server.MapPath("Image/" + strtemp));
Use it like this:
Image2.ImageUrl = "~/Image/" + strtemp;
Server.MapPath is used to get the physical path of a resource of your server. You need it for operations like saving files. However your physical path is not valid on the web. You should use the virtual path to specify the url.
You can fetch Image like this.:-
Image2.ImageURL = "~/Image/"+strtemp;
Hope this helps you.
Image2.ImageUrl = "~\\Image\\" + strtemp;
Use the above code.
if(!(DropDownList1.SelectedItem.Text==""))
{
string a = DropDownList1.SelectedItem.Text;
string [] q = a.Split('/');
string qq = q[1];
Image1.Visible = true;
Image1.ImageUrl = "~/Images1/" + qq;
}

Categories