-3

Is there any way i can take the data from the original location instead of copying the file to the bin/debug folder. I am looking to send a file to my local printer. But my C# application is allowing to send file only when i copy the file to my bin/debug folder. Is there a way i can over come!!!

Codes:

private string image_print()
{
    OpenFileDialog ofd = new OpenFileDialog();
        {
            InitialDirectory = @"C:\ZTOOLS\FONTS",
            Filter = "GRF files (*.grf)|*.grf",
            FilterIndex = 2,
            RestoreDirectory = true
        };

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string filename_noext = Path.GetFileName(ofd.FileName);
        string path = Path.GetFullPath(ofd.FileName);
        img_path.Text = filename_noext;

        string replacepath = @"bin\\Debug";
        string fileName = Path.GetFileName(path);
        string newpath = Path.Combine(replacepath, fileName);

        if (!File.Exists(filename_noext))
        {
            // I don't like to copy the file to the debug folder
            // is there an alternative solution?
            File.Copy(path, newpath);

            if (string.IsNullOrEmpty(img_path.Text))
            {
                return "";
            }

            StreamReader test2 = new StreamReader(img_path.Text);
            string s = test2.ReadToEnd();
            return s;
        }
    }
}

private void button4_Click(object sender, EventArgs e)
{
    string s = image_print() + Print_image();

    if (!String.IsNullOrEmpty(s) && 
        !String.IsNullOrEmpty(img_path.Text))
    {
        PrintFactory.sendTextToLPT1(s);
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Stacy Kebler
  • 180
  • 1
  • 3
  • 22
  • 1
    What do you mean, it doesn't let you? Do you get a runtime exception? – Grant Winney Oct 28 '14 at 19:35
  • @GrantWinney i mean.. for the codes written above is allowing me to do the job.. but i don't want to copy `System.IO.File.Copy(path, newpath);` the file to the debug folder.. I want to select the file and directly send to the machine, rather than copying to the debug folder. – Stacy Kebler Oct 28 '14 at 19:53
  • what are the values of img_path and path? – Adam47 Oct 28 '14 at 19:56
  • Why are you doing this `if (!File.Exists(filename_noext))`? Shouldn't it be `if (!File.Exists(ofd.FileName))`? – Rufus L Oct 28 '14 at 20:07

2 Answers2

1

Try this:

private string image_print()
{
    string returnValue = string.Empty;

    var ofd = new OpenFileDialog();
        {
            InitialDirectory = @"C:\ZTOOLS\FONTS",
            Filter = "GRF files (*.grf)|*.grf",
            FilterIndex = 2,
            RestoreDirectory = true
        };

    if (ofd.ShowDialog() == DialogResult.OK && 
        !string.IsNullOrWhiteSpace(ofd.FileName) &&
        File.Exists(ofd.FileName))
    {
        img_path.Text = Path.GetFileName(ofd.FileName);

        using (var test2 = new StreamReader(ofd.FileName))
        {
            returnValue = test2.ReadToEnd();
        }
    }

    return returnValue;
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

It looks like the underlying issue is caused by these lines:

filename_noext = System.IO.Path.GetFileName(ofd.FileName); //this actually does include the extension!! It does not include the fully qualified path
...
img_path.Text = filename_noext;
...
StreamReader test2 = new StreamReader(img_path.Text);

You are getting the full file path from the OpenFileDialog then setting img_path.Text to just the file name. You are then using that filename (no path) to open the file for reading.

When you open a new StreamReader with just a filename it will look in the current directory for the file (relative to the location of the EXE, in this case bin/debug). Copying to "bin/debug" will probably not work in any other environment outside of your machine as the EXE will probably be deployed somewhere else.

You should use the full path to the selected file:

if (ofd.ShowDialog() == DialogResult.OK)
{
    path = Path.GetFullPath(ofd.FileName);
    img_path.Text = path;
    if (string.IsNullOrEmpty(img_path.Text))
        return "";//
    StreamReader test2 = new StreamReader(img_path.Text);
    string s = test2.ReadToEnd();
    return s;
}
Mark
  • 11
  • 1
  • Its not that .. i have given the `img_path.Text = filename_noext;` for some other reason.. i need filename and extension there – Stacy Kebler Oct 28 '14 at 20:45
  • 1
    Right but you also need the full file path. Example : "c:\temp\somefile.jpg" you are working with a value like : "somefile.jpg" – Mark Oct 28 '14 at 20:52