Search code examples
c#wpfvisual-studio-2010visual-studiofile-copying

Always asking file not present in bin/debug folder


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

Solution

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