标签: openfiledialog

使用OpenFileDialog w /相对路径作为initialDirectory

我想给Initial目录赋予适当的值,所以它会打开我在项目中创建的文件夹(称为"Images").我必须使用相对路径,所以我的程序不依赖于我工作的计算机.但问题是我不知道如何访问这个文件夹...

有谁知道如何解决这个问题?

c# directory relative-path openfiledialog

2
推荐指数
1
解决办法
1万
查看次数

如何在c#中使用"打开/保存文件"对话框时防止资源泄漏

我们在桌面应用程序(C#)中使用save/opn文件对话框.当我们第一次打开对话框时,句柄增加100.关闭对话框后,句柄不会减少.从下一次起,手柄增加10左右,减少2到4.

我们尝试通过调用dispose并使其为null来减少句柄.并尝试使用块.但他们都没有解决这个问题.

你能告诉我任何解决方法吗?或者我们可以使用任何自定义控件

请就此提出建议

提前致谢

代码:代码是

SaveFileDialog objSaveDialog = new SaveFileDialog();
try
{

    objSaveDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    objSaveDialog.Title = "Save to Text File";
    //objSaveDialog.ShowDialog();
    DialogResult dlgResult = objSaveDialog.ShowDialog();
    objSaveDialog.Dispose();
    if (dlgResult == DialogResult.OK)
    {
        string strSaveFilePath = objSaveDialog.FileName;

        if (!string.IsNullOrEmpty(strSaveFilePath))
        {
            TextWriter myTxtWriter = new StreamWriter(strSaveFilePath, false);
            for (int index = 0; index < 10000; index++)
            {
                myTxtWriter.WriteLine("sample text.....................................");
            }

            myTxtWriter.Flush();
            myTxtWriter.Close();
            myTxtWriter.Dispose();

        }
    }
}
finally
{
    if (objSaveDialog != null)
    {
        objSaveDialog = null;
        //((IDisposable)objSaveDialog).Dispose(); …
Run Code Online (Sandbox Code Playgroud)

c# openfiledialog resource-leak

2
推荐指数
1
解决办法
1838
查看次数

OpenFileDialog可以接收文件名*.*?

我想 在文件名EditBox上OpenFileDialog用作选择带表达式(.或*.zip)的文件.

我必须覆盖OpenFileDialog吗?如果那时,请给我一个关于它的提示.

现在,如果我把文件名作为 . 然后单击"打开",OpenFileDialog不会返回任何内容.

抱歉我的英语不好.

在此输入图像描述

c# openfiledialog winforms

2
推荐指数
1
解决办法
1655
查看次数

如何获取所选文件的目录?

我有一个程序,用户选择一个带有OpenFileDialog的文件,我将该路径(ofd.FileName)存储到字符串FilePath中,我需要获取该文件所在的文件夹的名称,我该怎么做?

就像用户选择文件"C:\ Users\Name\Documents\hi.txt"一样,如何获取文件夹路径"C:\ Users\Name\Documents"?

c# directory openfiledialog winforms

2
推荐指数
1
解决办法
1381
查看次数

openfile对话框取消崩溃

我在c#中制作一个基本的文字处理器用于培训.现在我正在打开它的开放文件.我可以毫无问题地打开文本文件.但是当我打开打开文件对话框然后取消它时,它会崩溃:/

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
    var OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
    getRichTextBox().Text = OpenFile.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)

我知道这是因为流读取器没有什么可读的,但我不知道如何解决这个问题.

提前致谢!

编辑:谢谢!它工作得很完美:)

c# openfiledialog

2
推荐指数
1
解决办法
349
查看次数

如何将图像文件保存在项目文件夹中?

在我的Windows应用程序中,我有PictureBox一个Button控件.我想从按钮的OnClick事件中加载用户的图像文件,并将该图像文件保存在我项目中的文件夹名称"proImg"中.然后我想在中显示该图像PictureBox.

我写了这段代码,但它不起作用:

OpenFileDialog opFile = new OpenFileDialog();
opFile.Title = "Select a Image";
opFile.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
if (opFile.ShowDialog() == DialogResult.OK)
{
    try
    {
        string iName = opFile.FileName;
        string filepath = "~/images/" + opFile.FileName;
        File.Copy(iName,Path.Combine("~\\ProImages\\", Path.GetFileName(iName)));
        picProduct.Image = new Bitmap(opFile.OpenFile());
    }
    catch (Exception exp)
    {
        MessageBox.Show("Unable to open file " + exp.Message);
    }
}
else
{
    opFile.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

它无法将图像保存在"proImg"文件夹中.

在此输入图像描述

c# openfiledialog picturebox winforms

2
推荐指数
2
解决办法
4万
查看次数

无法在InitialDir中打开OpenDialog窗口

我在使用指定的initialdir启动OpenDialog窗口时遇到问题.我现在拥有的是什么

procedure TForm1.fileMenuLoadClick(Sender: TObject);
begin     
SetCurrentDir(StartDir);
      SetCurrentDir('Cases');
      OpenDialog.Filename := '';
      OpenDialog.InitialDir := GetCurrentDir;
      OpenDialog.Filter := 'Sparfiler (.dat)|*.dat';
      // -------------------------------
      if OpenDialog.Execute then
      begin
        GeometryClear;
        DerobModel.Filename := OpenDialog.Filename;
        DerobModel.Open;
        pressed := True;
        SetCurrentDir('../');
        DerobModel.HouseProperties.StringValue['CaseDir'] := GetCurrentDir;
        DerobModel.HouseProperties.StringValue['StartDir'] := StartDir;
        SetCurrentDir(StartDir);
        UpdateGeometryPanel;
        mainUpdateComboBox;
        UpdatePropertiesPanel;
        UpdateEnergyPanel;
        UpdateAbsorption;
        UpdateClimatePanel;
        UpdateClimate;
        mainHide;
        Geometry.IsSelected := True;
        GeometryPanel.Visible := True;
        TreeView1.Enabled := True;
        TreeView1.HitTest := True;
        DerobModel.HouseProperties.BoolValue['GlazeChange'] := False;
      end;
Run Code Online (Sandbox Code Playgroud)

运行此代码时,它会一直打开我打开的最后一个文件的文件夹.我读到解决方案是清除OpenDialog的FileName属性,但它不起作用.然而有趣的是,它正在处理来自我的应用程序的先前版本的代码.

procedure TForm1.fileMenuLoadClick(Sender: TObject);
begin
  SetCurrentDir(StartDir);
  SetCurrentDir('Cases');
  OpenDialog.Filename := '';
  OpenDialog.InitialDir := GetCurrentDir;
  OpenDialog.Filter := 'Sparfiler (.dat)|*.dat'; …
Run Code Online (Sandbox Code Playgroud)

delphi openfiledialog delphi-xe5

2
推荐指数
1
解决办法
3512
查看次数

如何在不加载文件扩展名的情况下将文件名作为列表框项加载到列表框中?

我加载的插件必须加载到左侧的列表框中,并且必须加载它们而不使用文件扩展名.

所以

"FirstPlugin.dll"

将加载为

"FirstPlugin"

当我在我尝试的代码中加载没有扩展名的文件名时,要么只加载名称而不执行,要么只加载带扩展名的文件名.

这是背后的代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using PluginContracts;
using System;
using System.IO;
using Microsoft.Win32;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Diagnostics;
using System.Linq;

namespace SimplePlugin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {

        Dictionary<string, IPlugin> _Plugins; // move to class scope


        public MainWindow()
        {
            InitializeComponent();
            _Plugins = new Dictionary<string, IPlugin>();

        }

        private void AssembleComponents(object sender)
        {


            string selection = "";
            if (sender is …
Run Code Online (Sandbox Code Playgroud)

c# listbox openfiledialog

2
推荐指数
1
解决办法
615
查看次数

获取OpenFileDialog选择的文件的文件扩展名

我使用的OpenFileDialog是允许用户选择文件.然后我如何获得他们选择的文件的扩展名?我需要根据文件类型执行不同的操作.例如,如果他们选择PDF文件,我需要启动一个PDF查看器,但如果它是图像,我需要在一个PictureBox.

vb.net openfiledialog recover

2
推荐指数
1
解决办法
3005
查看次数

.NET Core上的OpenFileDialog

在.NET Framework上,您可以将System.Windows.Forms.OpenFileDialog用于具有本机Windows UI的打开文件,但这仅适用于Windows.

.Net Core或其他替代方案有一个System.Windows.Forms.OpenFileDialog实现?

c# openfiledialog .net-core

2
推荐指数
3
解决办法
4923
查看次数