I am working on a winform with spreadsheetlight library.I want to create a excel file with specific name under a specific folder when user clicked on "Save to Excel" button. Ihave tried the below code so far.
string path = System.IO.Path.GetTempPath();
sl.SaveAs(path + "\\" + dosyaismi + ".xlsx");
FileInfo fi = new FileInfo(path + "\\" + dosyaismi + ".xlsx");
if (fi.Exists)
{
System.Diagnostics.Process.Start(#path + "\\" + dosyaismi + ".xlsx");
}
else
{
MessageBox.Show("Dosya bulunamadı!");
}
When program run and clicked that button first time it takes 2 minutes to create file.But without closing program I clicked that button again with different filename it creates file immediately.
I searched for 2 hours but ı didn't find any solution.Can anyone help what is the problem?
Related
I've started to encounter a problem with File.Copy. This works fine for my data creation script, managing to duplicate thousands of files with no issues. My problem occurs when trying to create temp files later in my code.
I have added the code sample below that isn't working correctly. I've tried numerous different ways to try to resolve this to no avail. What I am doing is copying some user data files created in a directory on the C drive into a temp folder inside that user data folder.
Code
foreach (string originalFile in OriginalDataFileNames)
{
string tempFile = originalFile;
TempDataFiles.Add(tempFile);
Console.WriteLine("GlobalDataCtrl: Original Data File: " + XWSDataDirectory + "\\" + tempFile);
Console.WriteLine("GlobalDataCtrl: Saved Temp Data File: " + tempPath + "\\" + tempFile);
File.Copy(XWSDataDirectory + "\\" + originalFile, tempPath + "\\" + tempFile);
}
Exit Error
The program '[6256] XtremeWrestlingSim.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
Any help is appreciated, thanks in advance!
SOLUTION:
FileStream outputFS = null;
FileStream inputFS = null;
outputFS = new FileStream(tempPath + "\\" + tempFile, FileMode.CreateNew, FileAccess.ReadWrite);
using (inputFS = new FileStream(XWSDataDirectory + "\\" + originalFile, FileMode.Open))
{
inputFS.CopyTo(outputFS);
}
outputFS.Close();
inputFS.Close();
Not sure how nicely formatted this is, but it works. Replace File.Copy with the above code.
You are using File.Create just before you call File.Copy, I think that is the issue, it is leaving an open stream.
Maybe removing the File.Create call will solve the issue. If not you could get the returned value (which is a stream) and close it before trying to copy.
The file is opened with read/write access and must be closed before it can be opened by another application.
See remarks https://msdn.microsoft.com/en-us/library/ms143361(v=vs.110).aspx
i have a project with a picturebox that by clicking on a button , i save picturebox.image , in a folder with "scan" name .
and if i insert an image with repetitive name in picturebox , when i save image , program overwrite the image in the folder .
but i want when i insert a same name image to the folder "scan" , i use a messagebox with message : " name of image is repetetive . please change the name of image " . help me please ..
here is my saving code :
pictureBox1.Image.Save(Application.StartupPath + "\\scan\\ " + txtDesc11.Text + ".jpg");
You need to check if the file currently exists before you save. I encourage you, instead of just asking us to solve your problem you instead search "How do i check if a file exists?" and then try to solve the problem yourself.
string filePath = Application.StartupPath + "\\scan\\ " + txtDesc11.Text + ".jpg";
if (File.Exists(filepath))
{
// show a message to the user
}
else
{
pictureBox1.Image.Save(filePath);
}
I'm trying to download files from a remote location. But right before the download, I get my file locations from a web service, also on a remote location.
The thing is, I get a degrading performance over time. The downloaded file numbers decrease from around 2k in 3 minutes to 300-400 in the same time after an hour or two and I have 250k files.
Is the service or the download a problem? Or both?
I download files as below after I get the names from the service,
try
{
using (WebClient client = new WebClient())
{
if (File.Exists(filePath + "/" + fileName + "." + ext))
{
return "File Exists: " + filePath + "/" + fileName + "." + ext;
}
client.DownloadFile(virtualPath, filePath + "/" + fileName + "." + ext);
return "Downloaded: " + filePath + "/" + fileName + "." + ext;
}
}
catch (Exception e) {
return"Problem Downloading " + fileName + ": " + e.Message;
}
if (File.Exists(filePath + "/" + fileName + "." + ext))
Bottleneck is probably here.
When you get hella tons of files in a single folder checking if file with this name already exists might need some time to complete.
So you might want to store files in different folders
The problem was the information put on the richTextBox and label.
The rtb was appended with info regarding whatever happened to each individual element. The Label showed at which element we were. Apparently the cpu can't handle it and this becomes a major problem when running for prolonged time. It ate so much cpu that it eventually killed the application. Removing or limiting their output solved almost all problems.
On the other hand, the slight degradation (1.5k to 1.2k per minute after 2.5 hours) of the download that still exists is still a mystery.
I have a template excel file to generate excel files from it.
My code is as follows (This part is to create a new excel file from the template):
string currentFN = PropertyFinalResult[0].Fecha;
string fixCurrentFN = currentFN.Replace('/', '_');
string currentTime = DateTime.Now.ToLongTimeString();
string fixCurrentTime = currentTime.Replace(':', '_');
string addToFileName = fixCurrentTime.Replace(' ', '_');
string newFN = fixCurrentFN + "-" + addToFileName;
string SourceFile = Request.PhysicalApplicationPath + "Template\\ExcelTemplate.xlsx";
string DestFile = Request.PhysicalApplicationPath + "Template\\" + newFN + ".xlsx";
//To keep FileName for posterior deletion
Session["sDestFile"] = DestFile;
try
{
File.Copy(SourceFile, DestFile);
}
catch (Exception ex)
{
lblErrorSavingToDB.Text = "Error: " + ex.Message;
lblErrorSavingToDB.Visible = true;
}
after that I open the new excel file, insert the records in it and then, stream the file to the user by doing this:
//Streaming file to client
string fileName = newFN + ".xlsx";
Response.Redirect("../Template/" + fileName);
Now, my question is, whether the user save or not the file, when should I delete the generated file? I would prefer once the user closes the popup window regarding Open or Save the file. But how to know when the user closes that window?
You can use TransmitFile and then close once the transmission is over. Example:
try
{
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(path.FullName) + "\"");
Response.AddHeader("content-length", path.Length.ToString());
Response.TransmitFile(path.FullName);
Response.Flush();
}
finally
{
File.Delete(Server.MapPath("~/"+tpacode+".zip"));
}
When to delete the files (or maybe it's better to say "how long to keep the files") is a question that is best answered by your application's business rules.
In the past, in low-traffic applications, I've used a "clean-up" routine to delete files older than a certain threshold. That clean-up gets performed when a new file is created, and at that time any file in the designated folder that was older than the threshold would be deleted.
I have File Upload form which users use to upload certain files on the server. The files are uploaded in path that look like this.
"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
After that I have a repeater which displays some data and have a hyperlink. I want to name the hyperlink"Download files(" + fileCount + ")" and onclicked it should display a regular Save As window. Can you give me some code to do that. I've never done something like this.
To get the number of files in the directory, you need to use IO. Then
string path = #"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
int numFiles = Directory.GetFiles(path).Length;
Then in your page_load, just change to .Text of the link to include the numFiles variable