How to open save dialog with custom file view - c#

I have a save file dialog in my C# application which opens user's desktop as default:
SaveFileDialog saveFileAddress = new SaveFileDialog
{
FileName = "MyXml",
CheckPathExists = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
OverwritePrompt = true,
Title = "Saveyour XML",
AddExtension = true,
ValidateNames = true,
Filter = "XML Document | *.xml"
};
As you see I've customized many of its options, But the one I can't figure out, is how to set default file view (thumbnail, tile, etc.. ) for it.
Is it possible programmatically?

Related

how to print file C#

I wonder how to print any printable document such as doc/docx/txt/pdf using C# (WPF/WinForm whatever). MSDN documentation shows how to print .txt files (like this one https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). But when i'm trying to print .docx i've got XML-like text. I need to print file from directory only. I don't want to edit it etc. So i wonder i can skip few steps and send my file to printer directly, can't i?
using (var pd = new PrintDialog())
{
pd.ShowDialog();
var info = new ProcessStartInfo()
{
Verb = "print",
CreateNoWindow = true,
FileName = #"D:\Desktop\00762.pdf",
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(info);
}

Save dialog retrieves more then one extension

How can I avoid the multiple extensions in the retrieved file name from save dialog?
I have filtered the dialog only to Rich Text File and .doc files.
When I change the selection in the combo Box below the text Box with file name, the extension is added to the file name instead of changing the existing extension.
SaveFileDialog dialog= new SaveFileDialog();
dialog.Title = "Please select the directory in which the document will be created.";
// set a default file name
dialog.FileName = my_File_Name;
// set filters
dialog.Filter = "RTF Files (*.rtf)|*.rtf | Wordfile (*.doc)|*.doc | Text Files (*.txt)|*.txt";
if (dialog.ShowDialog() == DialogResult.OK)
{
//selected folder path
string placeToSaveDocument = Path.GetFullPath(dialog.FileName);
}
That is the result:
myFileName.rtf.doc.rtf.txt
Problem : Your Filter string is not proper as it has spaces after extension string *.rtf and *.doc
Solution : You need to eliminate the space after extension strings.Remove spaces after *.rtf and *.doc
Try This:
dialog.Filter = "RTF Files (*.rtf)|*.rtf|Wordfile (*.doc)|*.doc|Text Files (*.txt)|*.txt";

load an adobe dynamic form file

I have a .pdf created through Adobe LiveCycle Designer (it's a dynamic pdf ) i want to add this pdf to my windows app
this is what i tried
File file1 = new File(fileName);
System.Xml.XmlDocument xfadoc = new System.Xml.XmlDocument();
xfadoc.LoadXml(fileName);
here's how i get filename
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "c:\\";
dialog.Filter = "pdf files (*.pdf) | *.pdf | All Files (*.*) | *.* | xdp files (*.xdp) | *.xdp ";
dialog.FilterIndex = 2;
dialog.RestoreDirectory = true;
dialog.CheckFileExists = true;
dialog.DefaultExt = "pdf | xdp";
fileName = dialog.FileName.ToString();
but when i click on open file button and browse to where i have stored it ; it doesnot even appear
Also when i try to load this file in my C# windows app it gives me an exception at the following line
xfadoc.LoadXml(fileName);
the exception says that
'Data at root level is invalid'
If i say that i have loaded the string (filepath name) someone please tell me how can i extract the xml part only form this dynamic file
Try Filter without spaces in extensions part.
dialog.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*|xdp files (*.xdp)|*.xdp";
LoadXml loads the document from the string parameter. You want to use the Load method.

Using SaveFileDialog with ClosedXML

I try to tell you my problem. with ClosedXML i have SaveAs() method, but when i use SaveAs(string name), it saves my excel document to some strange folder with some strange path. so i've decide to use savefiledialog to give user posibility to select folder and name for document. how can i use savefiledialog with closedXML?
SaveAs() also have SaveAs(Path path). Can i use it?
The "strange" folder is the folder your application is running from (since you're not specifying a path).
If you want you can use the SaveFileDialog to get the path and pass it to the SaveAs method.
var saveFileDialog = new SaveFileDialog
{
Filter = "Excel files|*.xlsx",
Title = "Save an Excel File"
};
saveFileDialog.ShowDialog();
if (!String.IsNullOrWhiteSpace(saveFileDialog.FileName))
workbook.SaveAs(saveFileDialog.FileName);
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Excel files|*.xlsx";
//serialVal is name of a variable, not necessary at all if you don't need a specific file name
saveFileDialog.FileName = serialVal;
if (saveFileDialog.ShowDialog() == true)
{
workbook.SaveAs(saveFileDialog.FileName);
workbook.Dispose();
return;
}

C# Excel Interop: Opening and Showing CSV file

Hey I'm writing a wrapper for the excel interop, I want to be able to open a csv file in excel and show it to the user. I've got the basics down, but when i set visible to true and excel shows up, all columns are jammed into the first, and the separating commas are showing.
here's my helper.
public MyExcel(string filePath, bool readOnly)
{
_app = new Excel.Application();
_workbooks = _app.Workbooks;
_workbook = _workbooks.Open(_filepath, 0, _readOnly, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", !_readOnly, false, 0, true, true, true);
}
public void Show()
{
_app.Visible = true;
}
any suggestions?
When i open the file by double clicking Excel processes everything properly.
You will need to use the OpenText method, instead of Open, if you want Excel to parse for delimiters. Details: http://msdn.microsoft.com/en-us/library/bb223513%28v=office.12%29.aspx
An example in C#: http://msdn.microsoft.com/en-us/library/c9838808.aspx
It is MUCH easier than that if all you want to do is open the file...
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo("excel.exe", "output.csv");
proc.Start();

Categories