This is kinda strange, let me try to explain it as best as possible:
When I create a new file and Save it, it saves correctly (test.xml).
When I make changes to this file and Save it, it saves correctly (to test.xml)
When I make changes again to this file or just choose Save As, it works correctly (newtest.xml)
However, when I do a file open, make changes to a file (test.xml) and click Save it is saving to (newtest.xml).
This is in my MainForm.cs
if (this.openEditorDialog1.ShowDialog(this) == DialogResult.OK && editForm != null)
{
editForm.Close();
editForm = new EditorForm(this);
editForm.OpenFile(this.openEditorDialog1.FileName);
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.MdiParent = this;
editForm.Show();
}
private void biFileSave_Click(object sender, EventArgs e)
{
if (!editForm.HasFileName)
{
if (this.saveEditorDialog1.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
editForm.FileName = this.saveEditorDialog1.FileName;
}
}
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
}
This is in my EditorForm.cs
public void OpenFile(string strFileName)
{
diagramComponent.LoadSoap(mainForm.openEditorDialog1.FileName);
this.FileName = mainForm.openEditorDialog1.FileName;
this.tabControl1.SelectedTab = DiagramTab;
}
I'm sure it has to do with the what I'm doing in the EditoForm but I can't seem to figure it out.
else
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
It looks like you want:
this.ActiveDiagram.SaveSoap(editForm.FileName);
It must have to do with mainForm.openEditorDialog1.FileName. Use a FileName property of the form that does the saving. When you open the file, set the fileName to mainForm.openEditorDialog1.FileName. When you SaveAs, set the FileName property there, too. This way, whenever the current file, changes you set the FileName property appropriately. Then, when it comes time to save the file, you always have the correct filename.
In summary, only use the .FileName property of the SaveAs dialog or the FileOpen dialog right after you use them.
Related
I have a WinForms app that has a little setup program that writes to Properties.Settings. The user needs to choose his notifyIcon icon from his hard drive. I can't just change it with
notifyIcon1.Icon = Properties.Settings.Default.userIcon;
because it throws up
"Cannot convert from "string" to "System.Drawing.Icon".
Can somebody correct me?
"The user needs to choose his notifyIcon icon from his hard drive".
Is the icon in a filename somewhere? If the user selects an icon from the hard driver, does he in fact select a file that contains an icon?
If that is the case, you should define Properties.Settings.Default.UserIcon as a string and save the name of the filename. Give your window a property that gets and sets the UserIcon.
private string UserIconFileName
{
get => Properties.Settings.Default.UserIcon;
set => properties.Settings.Default.UserIcon = value;
}
private Icon LoadUserIcon
{
string userIconFileName = this.UserIconFileName
if (!File.Exists(userIconFileName))
{
// TODO: decide what to do if there is no such file
}
else
{
return new Icon(userIconFileName);
}
}
Don't forget to Save your properties when closing the program:
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Save();
}
I am a beginner in programming and it may seem a little funny to ask questions like this, but i have this code :
protected void OnSaveActionActivated (object sender, EventArgs e)
{
FileFilter filter = new FileFilter();
filter.Name = "Text files";
filter.AddPattern ("*.txt");
FileChooserDialog fcd = new FileChooserDialog ("... ?", this,
FileChooserAction.Save, "Cancel", ResponseType.Cancel, "OK", ResponseType.Accept);
if ((int)fcd.Run () == (int)ResponseType.Accept) {
System.IO.StreamWriter sw= new System.IO.StreamWriter(fcd.Filename);
sw.Write(textview1);
fcd.Filter = filter;
fcd.Destroy ();
} else {
fcd.Destroy ();
}
fcd.Destroy ();
Tho my Open file version works perfectly, I cant seem to make files to save properly into txt files, and i just dont understand anymore.
Sos :(
If textView1 is an actual control, then
sw.Write(textview1);
will not do what you want. Instead, try
sw.Write(textview1.Text);
which will write out the actual contents of the control, not the control itself.
Also, as #HighCore mentioned, it is much simpler to do
File.WriteAllText(file_path, textView1.Text);
Writing a WP7.1 (Mango) app in Silverlight for Windows Phone (XAML / C#). I have an exception that I just cannot shake. I have a ListBox displaying an ObservableCollection<string> which displays the file names of XML files from IsolatedStorage. The Listbox control also contains a ContextMenu from the Windows Phone Toolkit. I use it to delete items from my ListBox, and from disk.
The ContextMenu and my delete method works fine normally... But if, whilst on the same page without navigating away, I create a new file (let's call it File1) then create another file (File2), if I then attempt to delete File1, the IsolateStorageFile.DeleteFile method throws an exception stating "An error occurred while accessing IsolatedStorage" with an inner message of null. HOWEVER, if I create File1, then File2. Then delete File2 then File1, it works just fine! ARGH!
If I leave the Page or restart the app again, I can delete the file no problems.
I've stripped back the code to hopefully make it a bit easier to read.
UI Binding Collection field in the code behind.
ObservableCollection<string> Subjects;
Click event calls the write method.
private void Button_Click_AddNewSubject(object sender, RoutedEventArgs e)
{
if (TryWriteNewSubject(NewSubjectNameTextBox.Text))
{
... Manipulate UI
}
}
Method to Add file to IsoStore and Subjects collection. Returns a bool for conditional UI manipulation.
private bool TryWriteNewSubject(string subjectName)
{
... file name error checking
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
store.OpenFile(subjectName + ".xml", FileMode.CreateNew);
store.Dispose();
}
Subjects.Add(subjectName);
return true;
}
else return false;
}
else return false;
}
The ContextMenu click event calls the delete file method
private void ContextMenuButton_Click(object sender, RoutedEventArgs e)
{
string subjectName = (sender as MenuItem).DataContext as string;
DeleteFile(subjectName);
}
And my delete method
private void DeleteFile(string subjectName)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
string fileName = subjectName + ".xml";
store.DeleteFile(fileName);
Subjects.Remove(subjectName);
}
}
The code is straight forward, I just don't know what I'm missing. :(
You get a IsolatedStorageFileStream from OpenFile. You need to dispose it before another operation can manipulate it.
Btw, a using statement calls dispose for you, so there's no need to call dispose at the end of a using statement.
I want to restrict what folder a person can choose to set their default save path in my app. Is there a class or method which would allow me to check access rights and either limit the user's options or show an error once they have made their selection. Is FileSystemSecurity.AccessRightType a possibility?
Since the FolderBrowserDialog is a rather closed control (it opens a modal dialog, does it stuff, and lets you know what the user picked), I don't think you're going to have much luck intercepting what the user can select or see. You could always make your own custom control, of course ;)
As for testing if they have access to a folder
private void OnHandlingSomeEvent(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK)
{
String folderPath = folderBrowserDialog1.SelectedPath;
if (UserHasAccess(folderPath))
{
// yay! you'd obviously do something for the else part here too...
}
}
}
private bool UserHasAccess(String folderPath)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds =
System.IO.Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
I should note that the UserHasAccess function stuff was obtained from this other StackOverflow question.
I want to be able to open a .txt file up into a richtextbox in c# and also into a global variable i have made called 'notes' but don't know how to do this. This is the code i have at the moment:
OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = opentext.FileName;
Globals.notes = opentext.FileName;
}
Only problem is it doesn't appear in neither the richtextbox nor in the global varibale, and the global allows it to be viewed in another richtextbox in another form. So please can you help, with ideally the .txt file going into both,
Thanks
Do you mean you want to have the text displayed or the filename?
richTextBox1.Text = File.ReadAllText(opentext.FileName);
Globals.notes = richTextBox1.Text;
You probably also want to correct this to:
if (opentext.ShowDialog() == DialogResult.OK)
In c# there are not global variables. The closest thing you can get is to make the variable "public static". But a better solution would be to make it an instance variable of an object you have access to, for example your main window class.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
FileName property of OpenFileDialog control just gives the full path of file selected by user. In order to read the content of this file, you will need to use a method like File.ReadAllText.
Try using this, I used it for a chat program and it works fine, you can set your timer rate to whatever you want. You also don't have to use a timer, you can have a button initiate the refresh of the rich text box.
private void refreshRate_Tick(object sender, EventArgs e)
{
richTextBox1.Text = File.ReadAllText(#"path.txt");
}
Hope this helps!
I hope, I am not late. It seems like there is something like:
OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
string selectedFileName = opentext.FileName;
richTextbox.LoadFile(selectedFileName, RichTextBoxStreamType.UnicodePlainText);
}
Refs