Problem with writing xml when openFileDialog is used

  • Thread starter Thread starter jasonMc
  • Start date Start date
J

jasonMc

Hey There,
Objective: To let the user to select a file through a openFileDialog
in c# then storing that information eg. Filename, Filepath and Date
Added to a xml file..
Im using Visual Studio 2008, here is an example of what im trying to
do this code works however i dont use a openFileDialog to select the
file hense :"E:\\blah";
DialogAddTo.Title = "Add Image to Dir to view";
DialogAddTo.InitialDirectory = "E:\\Primal\
\Images\\";
DialogAddTo.Filter = "JPG| *.jpg";
DialogAddTo.Multiselect = false;

String fileNameToAdd = "E:\\blah";
viwDataDS.imagesTable.AddimagesTableRow
(fileNameToAdd.Substring(fileNameToAdd.LastIndexOf("\\") +
1).ToString
(),
fileNameToAdd.ToString(),
DateTime.Now);
viwDataDS.imagesTable.WriteXml
("test.xml",
XmlWriteMode.WriteSchema);
When instead i use a fileDialogBox it doesnt work :( although all i
do
is add in the line showDialog i dont even use it. (but ofcourse i
want
to im just stressing the point)
DialogAddTo.InitialDirectory = "E:\\Primal\\Images\\";
DialogAddTo.Filter = "JPG| *.jpg";
DialogAddTo.Multiselect = false;

DialogAddTo.ShowDialog();

String fileNameToAdd = "E:\\blah";
viwDataDS.imagesTable.AddimagesTableRow
(fileNameToAdd.Substring(fileNameToAdd.LastIndexOf("\\") +
1).ToString
(),
fileNameToAdd.ToString(),
DateTime.Now);
viwDataDS.imagesTable.WriteXml
("test.xml",
XmlWriteMode.WriteSchema);
Oh and hense:
viwDataDS is the dataSet im using.. (this updates fine in both
occurances just the 2nd doesnt write the xml file :()
DialogAddTo is my openFileDialog
if anyone has any idea on my problem please post back spent hours
trying to debug it :(.
oh and the DialogAddTo i added to the form to create it.
Cheers
Jason
 
Hey There,
Objective: To let the user to select a file through a openFileDialog
in c# then storing that information eg. Filename, Filepath and Date
Added to a xml file..
Im using Visual Studio 2008, here is an example of what im trying to
do this code works however i dont use a openFileDialog to select the
file hense :"E:\\blah";
DialogAddTo.Title = "Add Image to Dir to view";
                        DialogAddTo.InitialDirectory = "E:\\Primal\
\Images\\";
                        DialogAddTo.Filter = "JPG| *.jpg";
                        DialogAddTo.Multiselect = false;

String fileNameToAdd = "E:\\blah";
                        viwDataDS.imagesTable.AddimagesTableRow
(fileNameToAdd.Substring(fileNameToAdd.LastIndexOf("\\") +
1).ToString
(),
                                fileNameToAdd.ToString(),
                                DateTime.Now);
                            viwDataDS.imagesTable.WriteXml
("test.xml",
XmlWriteMode.WriteSchema);
When instead i use a fileDialogBox it doesnt work :( although all i
do
is add in the line showDialog i dont even use it. (but ofcourse i
want
to im just stressing the point)
DialogAddTo.InitialDirectory = "E:\\Primal\\Images\\";
                        DialogAddTo.Filter = "JPG| *.jpg";
                        DialogAddTo.Multiselect = false;

DialogAddTo.ShowDialog();

String fileNameToAdd = "E:\\blah";
viwDataDS.imagesTable.AddimagesTableRow
(fileNameToAdd.Substring(fileNameToAdd.LastIndexOf("\\") +
1).ToString
(),
                                fileNameToAdd.ToString(),
                                DateTime.Now);
                            viwDataDS.imagesTable.WriteXml
("test.xml",
XmlWriteMode.WriteSchema);
Oh and hense:
viwDataDS is the dataSet im using.. (this updates fine in both
occurances just the 2nd doesnt write the xml file :()
DialogAddTo is my openFileDialog
if anyone has any idea on my problem please post back spent hours
trying to debug it :(.
oh and the DialogAddTo i added to the form to create it.
Cheers
Jason

http://social.msdn.microsoft.com/Fo.../thread/39cbc46a-2ad5-4300-8f08-4ca1469d6f76/

Found my answer:


DialogAddTo.RestoreDirectory = true;

added that line before .showDialog() and it fixed the problem...
 
http://social.msdn.microsoft.com/Fo.../thread/39cbc46a-2ad5-4300-8f08-4ca1469d6f76/

Found my answer:


DialogAddTo.RestoreDirectory = true;

added that line before .showDialog() and it fixed the problem...

Glad you were able to find the solution. For what it's worth, it seems to
me that your question has nothing at all to do with writing an XML file,
nor a complete description of what "it doesn't work" means. For future
reference, you may want to work harder to provide a clear, to-the-point
question if you want clear, to-the-point answers. You'll find that you
get much better results from your questions that way.

Pete
 
Found my answer:
DialogAddTo.RestoreDirectory = true;
added that line before .showDialog() and it fixed the problem...

Ultimately your problem is right here:

Never ever EVER use a "bare" file name when reading, writing, or performing
any other activity you can think of on a file. ALWAYS make sure you include
a path. Relying on the current working directory is a recipe for errors (and
subsequently confusing newsgroup posts).
 
Back
Top