picturebox.image saving overwrite in c# - c#

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);
}

Related

how to upload mp3 file using asp.net c#

I want to upload mp3 file using file uploader and I don't want to save it in the database I used server.mappath
if (FileUploadsong.HasFiles)
{
FileUploadsong.PostedFile.SaveAs(Server.MapPath("songs" + "/" + Txtsongname + "~/mp3file/"));
byte[] mp3file = System.IO.File.ReadAllBytes("songs");
}
but I got this error:
The error tells you that you path name is invalid and cannot be found.
That is, because you are using the control itself as part of your path, instead of its .Text property:
if (FileUploadsong.HasFiles)
{
FileUploadsong.PostedFile.SaveAs(Server.MapPath("songs" + "/" + Txtsongname.Text + "/mp3file/"));
byte[] mp3file = System.IO.File.ReadAllBytes("songs");
}
And please, post your errors as text, not as an image. Screenreaders cannot interpret images and if the image does get deleted, so does your error message for everyone.

How do you save and preview an HTML file from temp folder?

I'm developing an HTML editor in C# where you can edit your code in the FastColoredTextBox.dll component. You will have this option in the MenuStrip called "Preview in browser" and there will be a drop down item called "Chrome" and "Iexplore" etc. I want it instead of saving the file, i want it to make a file in the Temp folder and preview it. and after we've modified the code again, the file will update as we preview it again. This is what i have so far:
string location = null;
string sourcecode = FastColoredTextBox1.Text;
location = System.IO.Path.GetTempPath() + "\\TempSite.html";
using (StreamWriter writer = new StreamWriter(location, true))
{
writer.Write(sourcecode);
writer.Dispose();
}
try
{
System.Diagnostics.Process.Start("chrome.exe", location);
}
catch (Exception ex)
{
Interaction.MsgBox(ex.Message);
}
How do you achieve this?
Q: How do you save and preview an HTML file from temp folder?
A: You're already doing precisely that :)
Q: Why does my browser keep re-displaying the original image?
A: Because your browser is reading the html from cache.
SOLUTION:
Give your new file a different name. For example:
location = System.IO.Path.GetTempPath() + Path.GetTempFileName() + ".html";
... OR ...
location = Path.GetTempPath() + Guid.NewGuid().ToString() + ".html";
You can also simply hit <F5> to refresh, <Ctl-Shift-Del> to clear cache, or disable cache in your browser.

Delete an image programmatically after loaded in pictureBox completely in C#

I have an image file named image1.jpg in picture folder. This image file is protected by zip and the password is 1234. I need to load this image to my C# winform application; So I have to unzip this image, load it and then delete the image1.jpg again. My problem is that my C# application doesn't show the image when I delete it even after loading the image. If I remove "File.Delete(...." line as you see, It shows the image and there is no problem.
//Unzip a zip file protected and overwrite if needed
using (ZipFile zip = ZipFile.Read(Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.zip"))
{
zip.Password = "1234";
zip.ExtractAll(Directory.GetCurrentDirectory().ToString() + "\\Pictures\\", Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
}
pictureBox1.ImageLocation = Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg";
File.Delete(Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg");
var imagePath = Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg;
pictureBox1.LoadCompleted += (s, e) => File.Delete(imagePath);
pictureBox1.ImageLocation = imagePath;

File Uploading using Server.MapPath() and FileUpload.SaveAs()

I have a website admin section which I'm busy working on, which has 4 FileUpload controls for specific purposes. I need to know that , when I use the Server.MapPath() Method Within the FileUpload control's SaveAs() methods, Will it still be usable on the web server after I have uploaded the website? As far as I know, SaveAs() requires an absolute path, that's why I map a path with Server.MapPath()
if (fuLogo.HasFile) //My FileUpload Control : Checking if a file has been allocated to the control
{
int counter = 0; //This counter Is used to ensure that no files are overwritten.
string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' });
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]); // This is the part Im wondering about. Will this still function the way it should on the webserver after upload?
if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG")
{
while (System.IO.File.Exists(logo))
{
counter++; //Here the counter is put into action
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]);
}
}
else
{
cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again.";
cvValidation.IsValid = false;
}
if (fuLogo.PostedFile.ContentLength > 409600 ) //File must be 400kb or smaller
{
cvValidation.ErrorMessage = "Please use a picture with a size less than 400 kb";
cvValidation.IsValid = false;
}
else
{
if (fuLogo.HasFile && cvValidation.IsValid)
{
fuLogo.SaveAs(logo); //Save the logo if file exists and Validation didn't fail. The path for the logo was created by the Server.MapPath() method.
}
}
}
else
{
logo = "N/A";
}
If you intend to save the files in
a directory on your web server , then
the Server.MapPath() will be the suitable
solution.
string dirPath = System.Web.HttpContext.Current.Server.MapPath("~") + "/Images/Logos/"+ fileBreak[0] + counter.ToString() + "." + fileBreak[1];
Look Here
if you intend to save your files out
the web server then
Use a full path, like "c:\uploads"
and be sure that the web process has
permission to write to that folder,I suggest you store the path itself in the web.config file in this case.
yes, that can be used after saving file and when you try retrieve that file...
Server.MapPath("~/Images/Logos/" + uploadedFileName);
Yes it should still work as Server.MapPath uses the relative values and returns the complete physical path.
It's one line of code:
FileUpload1.PostedFile.SaveAs(Server.MapPath(" ")+"\\YourImageDirectoryOnServer\\"+FileUpload1.FileName);

Asp.net finding and downloading file

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

Categories

Resources