writing output to directory where exe is opened from

后端 未结 2 1431
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题