是否可以避免 SaveFileDialog 中的文件名验证?
var saveFileDialog = new SaveFileDialog {
Filter = "Job package (*.job)|*.job",
CheckPathExists = false,
ValidateNames = false };
var result = saveFileDialog.ShowDialog();
if (result.Value)
{
....my own validation....
}
Run Code Online (Sandbox Code Playgroud)
ValidateNames 属性不起作用。无论如何,当我输入包含无效字符的名称时,对话框会显示它自己的弹出窗口“文件名无效”。我可以避免吗?如何?
我们正在使用CustomPlacesOpen/SaveFileDialog 的属性向用户显示他们可以使用的各种配置的标准文件夹。
我们现在想要的是让InitialDirectory属性指向创建的CustomPlaces“文件夹”。有没有人能够做到这一点?
我已经阅读了有关FileDialog类(以及子序列 OpenFileDialog 和 SaveFileDialog)、InitialDirectory属性、Environment.SpecialFolder枚举器和KNOWNFOLDERID列表的 MSDN 文章,但都无济于事。
我还尝试将InitialDirectory属性设置为应用程序的名称,以防万一这很神奇。
用于 OpenFileDialog 的代码(目录是 demos atm):
using (OpenFileDialog tempDiag = new OpenFileDialog())
{
tempDiag.CustomPlaces.Add(@"C:\Temp\");
tempDiag.CustomPlaces.Add(@"C:\Program Files\");
tempDiag.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)
调用时所需的默认选择tempDiag.ShowDialog();:
我知道可以通过这篇CodeProject文章中描述的方法完全自定义 XOpenDialog ,但我觉得这对于此时的默认选择来说有点极端。
我有一个"保存"按钮,所以当用户点击时,它将保存xml文件(xml序列化).这里使用了savefiledialog,当我按下取消而不选择任何文件时出现"参数异常"并说"空路径名称不合法".我该如何处理这个例外?即使没有在savefiledialog中选择任何路径,我希望表单保持不变.非常感谢.
我的savefiledialog片段:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
string savepath;
SaveFileDialog DialogSave = new SaveFileDialog();
// Default file extension
DialogSave.DefaultExt = "txt";
// Available file extensions
DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
// Adds a extension if the user does not
DialogSave.AddExtension = true;
// Restores the selected directory, next time
DialogSave.RestoreDirectory = true;
// Dialog title
DialogSave.Title = "Where do you want to save the file?";
// Startup directory
DialogSave.InitialDirectory = @"C:/";
DialogSave.ShowDialog();
savepath = DialogSave.FileName;
DialogSave.Dispose(); …Run Code Online (Sandbox Code Playgroud) 我有一个数据网格视图,我已导出到Excel工作表.代码运行良好,但当出现另存为对话框并保存文件时,我找不到文件,也没有出现错误.
private void button1_Click(object sender, EventArgs e)
{
try
{
using (new ExcelUILanguageHelper())
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
Excel.ApplicationClass ExcelApp = new Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 30;
for (int i = 0; i < DGData.Rows.Count; i++)
{
DataGridViewRow row = DGData.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
ExcelApp.Cells[i + 1, j + …Run Code Online (Sandbox Code Playgroud) 我需要实现类似于Notepads的保存选项.假设我有一个按钮放在a旁边RichTextBox,我想要的是,当点击这个按钮时,会打开一个对话框,它看起来类似于单击另存为时出现的对话框.我想通过在" 保存对话框"中输入文件名来以文本格式保存RichTextBox的内容.
我的一位客户在保存文件时遇到了WPF应用程序崩溃的问题.
我的保存文件代码是:
var saveFileDialog = new SaveFileDialog {
InitialDirectory = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\MyApp"),
FileName = "MyFile",
OverwritePrompt = true,
AddExtension = true
};
if (saveFileDialog.ShowDialog() == true) {
...
}
Run Code Online (Sandbox Code Playgroud)
以下是他们获得的例外情况:
Value does not fall within the expected range.
A System.ArgumentException occurred
at MS.Internal.Interop.HRESULT.ThrowIfFailed(String message)
at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
at Microsoft.Win32.CommonDialog.ShowDialog()
Run Code Online (Sandbox Code Playgroud)
(ShowDialog最后一行中的位置是指我在上面的代码中进行的调用.)
所以我的预感是,在我的客户的情况下,对Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)的调用返回了一些SaveFileDialog不喜欢的内容InitialDirectory.我在Web搜索中找到(并验证)在传递InitialDirectorySaveFileDialog 的相对路径时发生此错误.是否有可能Environment.SpecialFolder.MyDocuments作为相对路径返回?如果没有,是否有人知道另一种可能无效的格式?某个SpecialFolder.MyDocuments网络路径可能是原因吗?还有其他想法吗?
我没有直接访问我客户的机器,他们不是特别精通技术,所以不可能100%确定发生了什么.
我正在使用Excel 2010.我正在尝试使用此代码保存我的excel文件.它确实保存了一个.xls文件,但是当我打开文件时,我收到了这条消息:
您尝试打开的文件"tre.xls"的格式与文件扩展名指定的格式不同.在打开文件之前,请验证文件是否已损坏且来自受损源.你想现在打开文件吗?
如果我按下yes文件打开.但是我需要摆脱这种格式弹出窗口?
我的代码:
using System;
using System.Windows.Forms;
using ExcelAddIn1.Classes;
using ExcelAddIn1.Classes.Config;
using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;
using System.Runtime.InteropServices;
private void Foo(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.FileName = null;
saveFileDialog.Title = "Save path of the file to be exported";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Save.
// The selected path can be got with saveFileDialog.FileName.ToString()
Application excelObj …Run Code Online (Sandbox Code Playgroud) 我用这段代码创建了Filter:
saveFileDialog1.FileName = "SimplifiedLog";
saveFileDialog1.Filter = "RichTextFormate | *.rtf |Text Files | *.txt |All Files| *.*";
saveFileDialog1.Title = "Save Simplified KL File";
saveFileDialog1.ShowDialog();
Run Code Online (Sandbox Code Playgroud)
问题是每次我选择任何过滤器(除了选定的过滤器)之外,它会在前一个过滤器中添加扩展名.见下图:
我尝试,当我按下保存时,SaveFileDialog我做了一些事情.我尝试修复但总是出错.
SaveFileDialog dlg2 = new SaveFileDialog();
dlg2.Filter = "xml | *.xml";
dlg2.DefaultExt = "xml";
dlg2.ShowDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
{....}
Run Code Online (Sandbox Code Playgroud)
但我有错误 - 可以说:
错误: 'System.Nullable'不包含'OK'的定义,并且没有扩展方法'OK'可以找到接受类型'System.Nullable'的第一个参数(你是否缺少using指令或汇编引用?)
我尝试修复此代码:
DialogResult result = dlg2.ShowDialog(); //here is error again
if (result == DialogResult.OK)
{....}
Run Code Online (Sandbox Code Playgroud)
现在错误在DialogResult上说: 'System.Windows.Window.DialogResult'是一个'属性'但是像'类型'一样使用
我正在使用savefiledialog来保存文件.现在我需要检查名称是否已存在.
如果存在,则用户需要有机会更改名称或覆盖现有文件.
我已经尝试了所有的东西并搜索了很多但是找不到解决方案,而我在技术上认为应该很容易.在if(File.Exists(Convert.ToString(infor)) == true)中必须进行检查.
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = ".xlsx Files (*.xlsx)|*.xlsx";
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = Path.GetDirectoryName(sfd.FileName);
string filename = Path.GetFileNameWithoutExtension(sfd.FileName);
for (int i = 0; i < toSave.Count; i++)
{
FileInfo infor = new FileInfo(path + @"\" + filename + "_" + exportlist[i].name + ".xlsx");
if (File.Exists(Convert.ToString(infor)) == true)
{
}
toSave[i].SaveAs(infor);
MessageBox.Show("Succesvol opgeslagen als: " + infor);
}
}
Run Code Online (Sandbox Code Playgroud) savefiledialog ×10
c# ×9
wpf ×5
.net ×1
datagridview ×1
dialog ×1
dialogresult ×1
excel ×1
file ×1
format ×1
notepad ×1
richtextbox ×1
validation ×1