在我的应用程序中,我有两个用户需要选择文件的地方.在这两种情况下,文件都在不同的目录中,但它们在运行之间通常是相同的.
OpenFileDialog似乎是默认使用的最后一个目录,但这对我来说没用,因为它几乎总是错误的文件夹,我最终在两个文件夹之间交替.我想要的是以某种方式让第一个对话框记住上次打开时使用的路径,第二个对话框也记住它自己的路径.
示例:路径A是C:\ foo\bar\something\x.dll,路径B是C:\ foo\baz\whatever\y.xml
对话框a打开,我选择A,然后稍后对话框b打开(默认为A),我必须向后导航到B.当我再次打开应用程序对话框打开(默认为B),我必须导航回来再来一次.
我想通过分别记住路径来避免所有这些额外的导航.有没有一个好方法呢?
我正在尝试使用 C#、Visual Studio 2010 中的 IFileOpenDialog 接口显示可以选择文件夹的标准打开文件对话框。
我正在尝试使用最少的代码,所以我只在接口中定义了我需要的方法:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Disable warning CS0108: 'x' hides inherited member 'y'. Use the new keyword if hiding was intended.
\#pragma warning disable 0108
namespace FolderDialog
{
internal static class IIDGuid
{
internal const string IModalWindow = "b4db1657-70d7-485e-8e3e-6fcb5a5c1802";
internal const string IFileDialog = "42f85136-db7e-439c-85f1-e4075d135fc8";
internal const string IFileOpenDialog = "d57c7288-d4ad-4768-be02-9d969532d960";
}
internal static class CLSIDGuid
{
internal const string FileOpenDialog = "DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7";
}
static class NativeMethods
{
[Flags]
internal …Run Code Online (Sandbox Code Playgroud) 有没有什么办法可以限制用户只从C#中的特定目录中选择文件?不应该允许浏览其他文件夹.
现在,我有一个设置类路径,但我想弹出一个打开的文件,用户选择打开哪个文件.我已经尝试过JFileChooser,但到目前为止还没有成功.这是我的代码:
public static void main(String[] args) throws IOException {
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog( null ) ;
if( returnValue == JFileChooser.APPROVE_OPTION ) {
File file = chooser.getSelectedFile() ;
}
// I don't want this to be hard-coded:
String filePath = "/Users/Bill/Desktop/hello.txt";
Run Code Online (Sandbox Code Playgroud)
我应该怎么做呢?
该FolderBrowserDialog不会允许我浏览网络上的计算机,但它显示其他不必要的文件夹(我不希望本地文件夹).此外,我不想选择一个文件夹 - 只是计算机名称.
// Browses file with OpenFileDialog control
private void btnFileOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogCSV = new OpenFileDialog();
openFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString();
openFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialogCSV.FilterIndex = 1;
openFileDialogCSV.RestoreDirectory = true;
if (openFileDialogCSV.ShowDialog() == DialogResult.OK)
{
this.txtFileToImport.Text = openFileDialogCSV.FileName.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我浏览要打开的文件.我想要做的是,浏览文件,选择它,然后按确定.单击确定后,我想复制选择的文件,并为该重复文件提供.txt扩展名.我需要帮助才能实现这一目标.
谢谢
我创建了一个控制台应用程序,让它以我想要的方式工作.使用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;
}
Run Code Online (Sandbox Code Playgroud)
我的静态主事件的那部分我希望从program.cs文件中创建"docPath"
static void Main(string[] args)
{
var excel = new ExcelQueryFactory();
excel.FileName = @"C:\Users\Christopher\Desktop\BookData\TestResults.xls";
<...code executed on opened excel file...>
}
Run Code Online (Sandbox Code Playgroud)
感谢您的时间.
这是我完成的解决方案:
class Program
{
[STAThread]
static void Main(string[] args)
{
var excel = new ExcelQueryFactory();
OpenFileDialog OFD = new OpenFileDialog();
OFD.Multiselect = false;
OFD.Title = …Run Code Online (Sandbox Code Playgroud) 为什么不显示过滤扩展OpenFileDialog?我测试了这个,但对话框没有显示DAT扩展.如果我DAT在列表扩展的第一个中获得扩展,则OpenFileDialog在对话框中过滤.我C#在Win XP上使用Application-FrameWork 3.5.
这是我的代码:
OpenFileDialog openFileDialog = new OpenFileDialog();
string VideoFormat = "Video files |*.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf; *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
" *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm; *.dat; "; …Run Code Online (Sandbox Code Playgroud) 我使用了这里的信息http://msdn.microsoft.com/ru-ru/library/system.windows.forms.openfiledialog(v=vs.110).aspx
这边走:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".xml"; // this is how I get only required extension
dlg.Filter = "XML files (*.xml)|*.xml"; // I guess, this should be modified, don't know how.
dlg.InitialDirectory = _directoryName1;
// here we go
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string path = dlg.FileName;
Run Code Online (Sandbox Code Playgroud)
在初始目录中,我必须输入具有相同xml扩展名的文件类型,名称以script-Data...或开头GeneralParam...。所以我只需要在 OpenFileDialog 中显示文件,名称以script-Data....
我知道,我可以通知用户,他通过解析确定了错误的文件path,但这对我来说不是一个好的解决方案。这里还有其他出路吗?
我正在使用多选文件对话框来浏览多个图片并将它们添加到datagridview然后从那里将它们存储在数据库中.
出于问题,我只设法存储了第一张选定的图片(根本没有语法或运行时错误).
经过检查,我意识到文件对话框仅获取第一张图像的完整路径,并将其用于其余图像.
示例代码:
if (ofd_pic.ShowDialog() == DialogResult.OK)
{
foreach (String file in ofd_pic.FileNames)
{
MessageBox.Show(ofd_pic.FileName);
}
}
Run Code Online (Sandbox Code Playgroud)
该消息框将始终仅显示第一个图像的路径,并且我无法获得每个所选图像的路径.
文件对话框的属性是:
1.Modifiers:私人.2. MultiSelect:True.3. RestoreDirectory:True.
有帮助吗?
openfiledialog ×10
c# ×9
winforms ×5
.net ×2
com-interop ×1
file-io ×1
interface ×1
java ×1
jfilechooser ×1