c#文件夹浏览器对话框问题

本文关键字:问题 对话框 浏览器 文件夹 | 更新日期: 2023-09-27 17:52:48

我有一个按钮,一旦点击使用stremReader读取文本文件,和一个文件夹浏览器对话框来保存文件。当我保存文件并再次点击按钮时,我得到一个错误,说它找不到文本文件,它试图从保存前一个文档的路径中读取文本文件。

有什么办法可以解决这个问题吗?

下面是部分代码:

private void Invoice_Load(object sender, EventArgs e)
{
    try
    {
        StreamReader sr = new StreamReader(@"../../DatabasePath");
        dataBase = sr.ReadLine();
        if (dataBase == null)
        {
            MessageBox.Show("Please use this to choose the location of the database.");
            Process.Start(@"..'..'DatabaseChooser.exe");
            ready = false;
        }
        if (!ready)
        {
            while (IsProcessOpen("DatabaseChooser"))
            {
                ready = false;
            }
            ready = true;
            if (ready)
            {
                doIfReady();
            }
        }
        else if (ready)
        {
            doIfReady();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
private void btnCreateInvoice_Click(object sender, EventArgs e)
{
    int SelectColumnIndex = 5;
    foreach (DataGridViewRow row in dataGridViewInvoice.Rows)
    {
        if (row.Cells[SelectColumnIndex].Value != null &&
               Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if (cell.OwningColumn.Index != SelectColumnIndex)
                {
                    data += (cell.Value + "          "); // do some thing                              
                }
            }
            data += System.Environment.NewLine;
            total += (int)row.Cells["TotPrice"].Value;
        }
    }
    MessageBox.Show("Please choose your invoice template", "Template");
    OpenFileDialog op = new OpenFileDialog();
    op.ShowHelp = true;
    op.Filter = "Microsoft Word Documents 97-2003 (*.doc)|*.doc|Microsoft Word 2007 (*.docx)|*.docx";
    if (op.ShowDialog() == DialogResult.Cancel)
    {
        this.Hide();
    }
    MessageBox.Show("Please choose where you want to save the invoice", "Save");
    FolderBrowserDialog fd = new FolderBrowserDialog();
    fd.Description = "Please choose";
    if (fd.ShowDialog() == DialogResult.Cancel)
    {
        this.Hide();
    }
    string path = fd.SelectedPath + "''" + txtFileName.Text + ".doc";
    CreateWordDoc(op.FileName, path);
}

c#文件夹浏览器对话框问题

首先,您可能想要发布一些代码。其次,您应该使用SaveFileDialog来保存文件,而不是使用FolderBrowser。

我认为你应该改变这部分

StreamReader sr = new StreamReader(@"../../DatabasePath");

使用绝对路径。
例如:

string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

这里的dir是主exe目录。
当你使用OpenDialog当前路径可以改变,所以你不会找到你的路径再次与../../

还有一件事:当你使用Process.Start(@"..'..'DatabaseChooser.exe");时,你等待你的过程完成:我认为如果你创建一个Process ps并使用ps.WaitForExit(),你可以做得更好。

我想你是有问题:

StreamReader sr = new StreamReader(@"../../DatabasePath"); 

改为:

File f = new File(@"../../DatabasePath");

然后执行f.GetAbsolutePath来找出它实际从哪里获取文件。

更多注释:

if (op.ShowDialog() == DialogResult.Cancel)
{
  this.Hide();                    
} 

如果用户点击取消,代码仍然会尝试运行。并尝试创建worddoc。