始终要求bin / debug文件夹中没有文件

时间:2014-10-28 19:28:20

标签: c# wpf visual-studio-2010 visual-studio file-copying

有什么方法可以从原始位置获取数据,而不是将文件复制到bin / debug文件夹。 我希望将文件发送到我的本地打印机。但是我的C#应用​​程序只允许在将文件复制到bin / debug文件夹时发送文件。有没有办法可以过来!!!

代码:

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

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

看起来潜在的问题是由这些线引起的:

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

您将从OpenFileDialog获取完整文件路径,然后将img_path.Text设置为文件名。然后,您使用该文件名(无路径)打开文件进行读取。

当您使用文件名打开一个新的StreamReader时,它将在当前目录中查找该文件(相对于EXE的位置,在本例中为bin / debug)。复制到“bin / debug”可能不适用于您机器之外的任何其他环境,因为EXE可能会部署在其他地方。

您应该使用所选文件的完整路径:

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;
}
相关问题