Tag: openfiledialog

如何将表单添加到控制台应用程序以便用户可以选择文件?

我已经创建了一个控制台应用程序,并按照我想要的方式工作。 使用VS2010中的“添加项目”>“添加Windows窗体”选项,它自动创建了我需要的内容。 我添加了一个按钮和代码来检索Excel文件(如下所示)我的问题是: 如何获取他们创建的文件并在program.cs“Main”区域中使用它? 来自Form1.cs的OpenFileDialog按钮单击事件的代码: private void btnSelect_Click(object sender, EventArgs e) { OFD.openFileDialog OFD = new OpenFileDialog(); OFD.Multiselect = false; OFD.Title = “Open Excel Document”; OFD.Filter = “Excel Document|*.xlsx;*.xls”; OFD.ShowDialog(); string docPath = OFD.FileName; } 我的静态主事件的那部分我希望从program.cs文件中创建“docPath” static void Main(string[] args) { var excel = new ExcelQueryFactory(); excel.FileName = @”C:\Users\Christopher\Desktop\BookData\TestResults.xls”; } 感谢您的时间。 这是我完成的解决方案: class Program { […]

如何在openFileDialog中保存最后一个文件夹?

如何使我的应用程序存储在openFileDialog打开的最后一个路径以及新的打开恢复后呢? OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = “txt files (*.txt)|*.txt|All files (*.*)|*.*”; if (openFileDialog1.ShowDialog() == DialogResult.OK) { acc_path = openFileDialog1.FileName; Settings.Default.acc_path = acc_path; foreach (string s in File.ReadAllLines(openFileDialog1.FileName)) { accs.Enqueue(s); } label2.Text = accs.Count.ToString(); }

在Windows XP中使用“打开文件”对话框时,为什么当前工作目录会更改?

我在c#中使用打开的文件对话框时发现了一个奇怪的行为。 如果在Windows XP使用此代码,则当前工作目录将更改为所选文件的路径,但是如果在Windows 7运行此代码,则当前工作目录不会更改。 private void button1_Click(object sender, EventArgs e) { MessageBox.Show(string.Format(“Current Directory {0}”,Directory.GetCurrentDirectory()), “My Application”,MessageBoxButtons.OK, MessageBoxIcon.Asterisk); DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog and get result. if (result == DialogResult.OK) { } MessageBox.Show(string.Format(“Current Directory {0}”, Directory.GetCurrentDirectory()), “My Application”, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } 有人知道这种行为的原因吗? 为什么当前目录在XP中而不是在Windows 7更改?

继续打开OpenFileDialog,直到选择有效文件

我有打开OpenFileDialog的代码,我正在检查文件的大小以确保它不超过特定限制,但如果用户选择了一个大文件大小,我需要警告他,并让他回到对话框,选择其他文件,或单击取消。 这是最好的方法: OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = “Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx”; while (dialog.ShowDialog() != DialogResult.Cancel) { var size = new FileInfo(dialog.FileName).Length; if (size > 250000) { MessageBox.Show(“File size exceeded”); continue; } } 编辑:在尝试之前我尝试了以下代码: OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = “Jpeg files, PDF files, Word files|*.jpg;*.pdf;*.doc;*.docx”; dialog.FileOk += delegate(object s, CancelEventArgs […]

检查文件扩展名是否为图像的好方法

我有这个文件类型filter: public const string Png = “PNG Portable Network Graphics (*.png)|” + “*.png”; public const string Jpg = “JPEG File Interchange Format (*.jpg *.jpeg *jfif)|” + “*.jpg;*.jpeg;*.jfif”; public const string Bmp = “BMP Windows Bitmap (*.bmp)|” + “*.bmp”; public const string Tif = “TIF Tagged Imaged File Format (*.tif *.tiff)|” + “*.tif;*.tiff”; public const string […]

如何使用C#将数据从一列Excel导入到列表框

我有一个openFileDialog工具。 我将从我的计算机中选择一个excel文件,我的程序读取一列(例如A列)并在GUI上编写我的列表框。 我怎么能通过OleDB做到这一点? 我是C#的新手。 如果你详细解释,我会很高兴。 谢谢您的帮助。

通过openFileDialog打开Web文件夹(Sharepoint 2007)

我有以下用C#编写的代码片段: openFileDialog1.InitialDirectory = “\\\\fwm-storage\\users\\” + curUser + “\\My Documents\\My Pictures”; openFileDialog1.Filter = “All Files (*.*)|*.*|Images (*.jpg)|*.jpg”; openFileDialog1.FilterIndex = 2; // blah, blah, blah 哪个很好用,但是,我希望能够添加/更改将openFileDialog.InitialDirectory作为我们的SharePoint服务器上的Web文件夹的function。 比如说:“http:\ intranet \ company \ division \ photos” 任何人都对如何做到这一点有任何想法。 我已经筋疲力尽了早上寻找答案,而我的GoogleFu显然没有达到标准。 我看到了稀疏的建议,但看起来它可能需要WPF(Windows Presentation Foundation)或安装Visual Studio的Sharepoint SDK(可能(?)允许我直接访问SharePoint dBase)。 我不是在安装SDK的上方,如果这将使我到达我需要去的地方; 而不是WPF上的那么多。 任何帮助是极大的赞赏。 即使只是说它无法完成。 如果您需要更多信息,请随时高兴。 谢谢。 Jasooomian

设置openfile对话框的默认文件夹

我有一个OpenFileDialog ,我正在尝试设置默认文件夹。 最初我将它设置为Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @”\\new_folder1″并且运行良好。 但是我将它更改为Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @”\\new_folder2″ ,它仍然会在new_folder1中弹出。 当我调试它时,对话框的InitialDirectory是new_folder2。 我删除了new_folder1,但对话框在启动时仍然会查找它。 我的代码中的任何地方都没有引用new_folder1。 关于可能发生的事情的任何想法? 编辑:这是我设置初始OpenFileDialog的代码: OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = “XML files (*.xml)|*.xml”; String pathDefault = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @”\\new_folder2″; dlg.InitialDirectory = pathDefault;

可以设置.NET OpenFileDialog以允许用户选择.lnk文件

我想显示一个允许用户选择快捷方式(.lnk)文件的对话框。 我的问题是对话框尝试获取快捷方式指向的文件/ URL,而不是.lnk文件本身。 如何让它允许选择.lnk文件?

取消openfiledialog时如何防止exception?

我的程序有一个按钮,当点击它打开一个openfiledialog来选择一张图片: private string ChoosePicture() { fDialog.Title = “Select Picture”; fDialog.Filter = “Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg”; fDialog.InitialDirectory = “C:”; fDialog.ShowDialog(); fDialog.AddExtension = true; fDialog.CheckFileExists = true; fDialog.CheckPathExists = true; //returns a string for the directory return fDialog.FileName.ToString(); } 使用dialogresult框上的检查还没有解决我的问题: fDialog.AddExtension = true; fDialog.CheckFileExists = true; fDialog.CheckPathExists = true; DialogResult res = fDialog.ShowDialog(); if (res […]