最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

winforms - I have a problem with picking up a file in a Visual Studio Console App - Stack Overflow

programmeradmin3浏览0评论

I've got some legacy code in C++ (working) that I want to use from a C# Winforms application. What I've done is call a C++ console app after writing a data file in the Winform app for the fitting routine in the console app. I'm using 2022 Visual Studio (Community)

Here's the relevant code where I create the file in Winforms and write it to the console.



using System.Text;
using System;
using System.IO;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Security.Cryptography;
using Microsoft.VisualBasic.ApplicationServices;
using System.Diagnostics;
using System.Runtime.InteropServices;

-other code

string folder = @"C:\Users\hugon\source\repos\4thDegreeSolver\4thDegreeSolver\";
// Filename
string fileName = "ToSolver";
// the fullpath
string fullPath = folder + fileName;
try
{
    
    StreamWriter sw = new StreamWriter(fullPath, false, Encoding.ASCII);
    //write the file to the fitting file for the simplex fitter
    for (int i = 0; i <= lineNumFit - 1; i++)
    {
        string dataLine = xInput[i].ToString() + '\t' + yInput[i].ToString();
        //richTextBox1.AppendText(dataLine+'\n');
        sw.WriteLine(dataLine);
    }
    sw.Close();
    sw.Dispose();
}
catch (Exception ex)
{
    MessageBox.Show("Exception: " + ex.Message);
}

Here's where I call the console app from Winforms:

private void runBTN_Click(object sender, EventArgs e)
{
    //run the  routine in a console
    var processSolve = new ProcessStartInfo(@"C:\Users\hugon\source\repos\4thDegreeSolver\Debug\4thDegreeSolver.exe");
    processSolve.UseShellExecute = true;
    Process.Start(processSolve);
    }
}

That works fine and the file is indeed sent to the appropriate folder for the console app. The console app tries to read the file in with the following code.

input in the console app

#include "inputData.h"
#include <fstream>
#include <iostream>
namespace Measurement
{
    std::vector<double> dataX;
    std::vector<double> dataY;

    void ReadDataXY(std::string filename)
    {
        using namespace std;
        ifstream datafile(filename.c_str());
        if (!datafile)
        {
            std::cerr << filename << ": no such file exists" << endl;
        }

        double x, y;
        while (datafile >> x, datafile >> y, !datafile.eof())
        {
            dataX.push_back(x);
            dataY.push_back(y);
        }
        datafile.close();
    }
}

Header file for the input:

//inputData.h
#include <vector>
#include <string>
namespace Measurement
{
    extern std::vector<double> dataX;
    extern std::vector<double> dataY;
    extern void ReadDataXY(std::string);
}

That file is then picked up by the main program here:

int main(int argc, char* argv[])
{
    using Measurement::ReadDataXY;
    string filename = "ToSolver";
    ReadDataXY(filename);
}

The console app always returns "no such file exists" when I call it from the Winform app.

The console app, when run separately creates a file with the solutions in it that are accurate and valid

When I run the console app separately it picks up the data file just fine from the one created by the Winform app. I have also written the file to different locations in the console app with no luck. I have done this before in older versions of Visual Studio and it worked just fine.

What am I missing?

Any ideas or thoughts would be well appreciated.

发布评论

评论列表(0)

  1. 暂无评论