writing output to directory where exe is opened from

后端 未结 2 1432
时光取名叫无心
时光取名叫无心 2021-01-26 07:32

I have a form exe. now on button click i want it to save a file to where it has been opened from, e.g. if i give this exe to you and you copy it to c

相关标签:
2条回答
  • 2021-01-26 08:06

    You can have a look at using AppDomain.BaseDirectory Property

    Gets the base directory that the assembly resolver uses to probe for assemblies.

    You also need to look at using Path.Combine Method (String, String)

    Combines two strings into a path.

    0 讨论(0)
  • 2021-01-26 08:20
    myVar.Save(@"test.xml");
    

    This should write test.xml to the directory your exe runs from.

    Note that in case of writing to c:\ root folder might require elevated administrator permissions.

    You didn't specify the type of myVar, but this might be helpful:

    using System.IO;
    
    class Program
    {
        static void Main(string[] args)
        {
            File.WriteAllText("HelloWorld", "test.txt");
        }
    }
    

    This creates test.txt file with HelloWorld content, the file is saved in the executable directory.

    0 讨论(0)
提交回复
热议问题