Long path to Python script when executing it from c#
<p>I am trying to run Python script from C# program. I use official documentation from Microsoft: <a href="https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee" rel="nofollow">https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee</a> When I pass short file path to my Python script as command argument it works fine. But when I enter long path to the same Python script, process runs, but script does not execute. Whats wrong? Here is the code I use:</p>

<pre><code>using System;
using System.IO;
using System.Diagnostics;

namespace CallPython
{
    class Program
    {
        static void Main(string[] args)
        {
            // full path of python interpreter 
            string python = @"C:\Anaconda2\python.exe";

            // This path will work
            string myPythonApp = @"C:\MyPython\helloworld.py";
            // This path will cause program to fail, nothing response 
            string myPythonApp = "C:\\Users\\My Name\\Documents\\Visual Studio 2015\\Projects\\My Project Name\\helloworld.py";

            // Create new process start info 
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

            // make sure we can read the output from stdout 
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;

            myProcessStartInfo.Arguments = myPythonApp;

            Process myProcess = new Process();
            // assign start information to the process 
            myProcess.StartInfo = myProcessStartInfo;

            Debug.WriteLine("Calling Python script: " + myPythonApp);
            // start the process 
            myProcess.Start();

            StreamReader myStreamReader = myProcess.StandardOutput;
            string myString = myStreamReader.ReadLine();

            myProcess.WaitForExit();
            myProcess.Close();

            // write the output we got from python app 
            Debug.WriteLine("Value received from script: " + myString);

        }
    }
}
</code></pre>

<p>This error can be caused by if your path is spelled wrong. For creating path dynamicly you can try the foling:</p>

<ol>
<li><p>You can try:</p>

<pre><code>Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).
</code></pre>

<p>This will return the path to the documents folder
<em>(also if other users will  use the program the path will be generated correctly)</em>.
So you can leave this part of the path: "C:\Users\My Name\Documents\" over to the compiler. From here you can continue to build the path to your script.</p></li>
<li><p>you can try:</p>

<pre><code>string path = "script.py";
</code></pre>

<p>If you have this as path it will read the file from here</p></li>
</ol>

<p>"C:\Users\My Name\Documents\Visual Studio 2015\Projects\My Project Name\My Project Name\bin\Debug\<strong>script.py</strong></p>

<p><em>Or where your executable is located.</em></p>

<ol start="3">
<li>you can use the Directory.GetParrent() Directory.GetCurrentDirectory() to get the current path and move to the parrent directory en work back to the script where you've stored it.</li>
</ol>

<blockquote>
  <p>These options will make it easier to create path dynamic.</p>
  
  <p>Take a look at this page for the directory class with has some nice features :
  <a href="http://msdn.microsoft.com/en-us/library/system.io.directory.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.io.directory.aspx</a></p>
  
  <p>Or the envoirment class with has some nice feuaters to generate paths:
  <a href="https://msdn.microsoft.com/en-us/library/system.environment(v=vs.110).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/system.environment(v=vs.110).aspx</a></p>
</blockquote>

c# python