For my system, i first need to read CSV file and then i need to connect to the database in SQL server 2008 according to the data in .ini file.
I use System.IO.FileStream() to read file. There is no problem to open and read data.
But, when i read csv file and then connect to the database , i cant access to the .ini file
because System.IO.FileStream() function take the path of ini file like as the location of csv file.
So, when i read csv file from Desktop , System.IO.FileStream() function search the ini file in Desktop and when i read from My Document, it search in My Document.
Thus, i want to know how to control this.
My function to read ini file : System.IO.FileStream("fileNameOnly", System.IO.FileMode.Open, System.IO.FileAccess.Read);
You can set your OpenFileDialog.RestoreDirectory to True to have the dialog reset its location on each use.
Related
I have made a windows service in C#, what this service does is read a text file and convert it to json, but what I want to know is how can I delete that txt file once converted to json, it doesn't allow me to delete it because I get this log, "The process cannot access the file 'C:\API\inbox\13203530500100000060800712202231100.TXT' because it is being used by another process."
I am using the following to get the txt of the folder:
string[] files = Directory.GetFiles(rutaRaiz, "*.txt");
thanks for the support,
use the File.Delete(item) to delete the txt but it gave me error:
, "The process cannot access the file 'C:\API\inbox\13203530500100000060800712202231100.TXT' because it is being used by another process."
I am using the lines below in the web service to see the XML which we send to another party:
XDocument doc;
doc.Save("san.xml");
In which directory can I find the san.xml file?
When saving a file in c#, if no directory is specified, the application writes to the running directory, which is normally where the application is.
I want to change the uploaded pdf's file name prior to saving on Server and saving the name/reference in my database.
In my controller I have this:
var pdf = System.Web.HttpContext.Current.Request.Files["myPDF"];
I want to change the file name, then I'll save the file and do the database work. How do I change the file name?
It technically doesn't have a file name until it's written to the file system. Anything it has in-memory is just meta-data associated with the byte stream. In the context of an HttpPostedFile those meta-data properties appear to be read-only.
Presumably at some point in your code you're saving the file. That's where you'd specify the file name:
var pdf = System.Web.HttpContext.Current.Request.Files["myPDF"];
pdf.SaveAs("anyCustomFileName.pdf");
In my project my app receives an XML from another program. I want to save this XML file in the folder in my PC. But if there is another XML file that will come, the XML will be added in the folder not overwrite the existing XML. How can I achieve it?
I am using this code right now but I don't know why I got an access denied error.
System.IO.File.WriteAllText(#"C:\XML", abc);
First of all, the error that you receive is because of Windows UAC which denies users without Administrator privileges to write on the C:\ disk. Create a folder in which you have access, for example "c:\temp\" and write your files in there.
Also, you specify a file and not folder.
And if you don't want to overwrite the file, just make sure to generate a unique filename (a guid perhaps).
I have deployed my software on a server,In my software there is a tool which reads an excel file and displays the content in a gridView.
It's working fine on my stand-alone PC, how can I do it on the web?
Should I upload my excel files on server and then read it or directly read it from the user's PC?
Any solutions?
you need to put fileupload control on your page
<asp:FileUpload ID="fup" runat="server" />
and then in code behind, save this file
fup.SaveAs(Server.MapPath("~/upload/temp/" + fup.FileName ));
you can take a look at this article
Performing a File Upload using ASP.NET 2.0 and C# .NET
and Simple File Upload Control with ASP.NET and C#
I am not getting your question so far.Which error occurs on web?
Here is the sample code which may help you.
Every time it uploads excel file on server from client machine and get data from that and bind grid.
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsyncFileUpload1.HasFile)
{
string strPath = MapPath("~/Uploads/") + System.IO.Path.GetFileName(e.filename);
// gets extension of a file to check for a valid excel file
string ext = System.IO.Path.GetExtension(e.filename);
if (ext.ToLower() == ".xlsx" || ext.ToLower() == ".xls")
{
AsyncFileUpload1.SaveAs(strPath);
}
DataTable dt = getdata(strPath); // get data from excel file
BindGrid(dt);
}
}
You can't read it directly from users PC, you need to upload files anyway.
It's working fine on your local environment because effectively your website and the file you're trying to read are on the same machine. As a result, the web server can just load the file directly and there's no problem.
In a live environment, the website will be on a server somewhere while the excel file will be on the user's on own pc. In this scenario the web server can't access the file and you will have to upload to the web server by means of a file upload dialog. You can then save this file to a safe folder you specify (ideally in your web.config in case you change your mind or need to move it) somewhere on your server and access the file to display, manipulate or even insert into a database should you choose to.
I don't think you should save it to your server unless you really have to (which would require Write permissions on the directory you save to). If you are using Open XML to read the Excel file you can read the posted stream directly without saving to file first via SpreadsheetDocument.Open(FileUpload1.PostedFile.InputStream, false)...