开发者

C# folderbrowser dialog problem

I have a button that once clicked uses a stremReader to read a text file, and a folderbrowser dialog to save a file. Once I save the file and and click on the button again I get an error saying it cannot find the text file, and i开发者_开发问答t tries to read the the text file from the path where the previous document was saved.

Is there any way I can resolve this problem?

Here is a part of the code:

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);
}


First of all you might want to post some code. Second of all, you should use the SaveFileDialog to save files instead of FolderBrowser.


I think you should change this part

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

using an absolute path.
For example:

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

Here dir is main exe dir.
When you use OpenDialog current path could be changed, so you won't find your path again with ../../.

Just another thing: when you use Process.Start(@"..\..\DatabaseChooser.exe"); you wait for your process to be finished: I think you could do better if you create a Process ps and use ps.WaitForExit().


I'm assuming you are having trouble with:

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

Change it to:

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

Then do a f.GetAbsolutePath to find out where it is actually getting the file.

A few more comments:

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

If the user hits cancel the code is still going to attempt to run. And try to CreateWordDoc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜