How would you make a Save button that, unlike a SaveAs button, will save over the previously selected file without opening a dialog? I have no trouble utilizing SaveAs to open a Save Dialog and create a file but automatically saving to a previously set text file has stumped me.
did you try something like that :
System.IO.File.AppendAllText(previously file path, the new value);
Related
I'm using this code for making save as of image in c#
SaveFileDialog svf = new SaveFileDialog();
svf.Filter = "JPEG files (*.jpeg)|*.jpeg";
if (DialogResult.OK == svf.ShowDialog())
{
this.imgbox.Image.Save(svf.FileName,ImageFormat.Jpeg);
}
I need to make save now for image without change name or location ( apply save not save as as the above code ) how i can use this code for save now ?
Assuming WinForms, add a field or property to the appropriate control to hold the full path to the file currently opened, and call it currentFileName or something.
When opening a file, assign the path to the variable. Now when the user hits the Save menu, you can use the already stored path to overwrite the opened file. You can add a Save As menu that pops up the SaveFileDialog, and afterwards assigns the result to the variable again - if not cancelled.
When the user invokes the New or Close menu, make sure to clear the stored path.
My problem is Saving Button location in XML file. My Form have button that are draggable on edit form, so when i am finished editing the form i want to save Button location in XML file, so then i can read the location from the XML file and display the button on the modified position.
Any Suggestions?
Unless it's otherwise a requirement for your application or you need to store additional complex data, you don't really need to create an XML file. You could use a plain text file like this:
var fileContents = string.Format("{0}\r\n{1}\r\n", button.Top, button.Left);
File.WriteAllText("ButtonCoordinates.dat");
Then to read and apply back the coordinates:
var fileContents = File.ReadAllLines("ButtonCoordinates.dat");
button.Top = double.Parse(fileContents[0]);
button.Left = double.Parse(fileContents[1]);
Of course you need to add error checking, file path management et al, but you get the idea.
I have 3 open file buttons which open a file open dialog, whenever one file is opened the starting directory for the next button is always the same as the last button that was used.
I need to be able to have each button open only the last directory that it was associated with, not what the last button opened was associated with.
How can make each dialog open in the directory that that specific dialog was opened in last?
Example, I have 3 buttons i want to open in the following order:
Btn1 Open File in dir C:\temp\1 then
Btn2 Open File in dir C:\temp\1 then change to C:\temp\2
Btn3 Open File in dir C:\temp\2 then change to C:\temp\3
Btn1 Open File in dir C:\temp\1 NOT in C:\temp\3
declare some private fields in your class:
string startLocationForDialog1 = "C:\";
string startLocationForDialog2 = "C:\";
string startLocationForDialog3 = "C:\";
Then in your methods, when you create the open file dialog, set the starting location to the value of the corresponding variable.
After the file is selected, save the location of the file (without the file name) in the corresponding variable. Next time you press the same button, you use that variable which contains the last location from which a file was selected.
May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.
Hi all - I need the user to select a file from a Dialog. I then only need the path to that file. I've been looking and the only way it seems is to use the <input> but i can't have the user click the choose file button, it must be "clicked" programmatically
Any help would be useful.
PS. THIS IS MY SECOND DAY OF ASP>NET - i also dont know javascript
Aiden
You can use jQuery to apply an event handler to the change event for the input type="file", but some browsers will return the file path name to the file, some will display a dummy path (with correct file name), and others will just give the file name with no path.
EDIT:
To click the file dialog programatically, you can hide it with css using display: none, and then do $("#<%=myFileInput.ClientID%>").click(); to prompt the dialog.