locked
Save Active Document DTE VSIX extension RRS feed

  • Question

  • i'm creating VS 2017 extension. i create file using ItemOperationd.NewFile and add some content to the file but that file exists on temp directory so i want to save that file in to specific location how can i do it

    here is file create code

    dte.ItemOperations.NewFile(@"General\Text File", docName, EnvDTE.Constants.vsViewKindTextView);

    Friday, June 9, 2017 7:27 AM

All replies

  • Hi Gayan85,

    >>i want to save that file in to specific location how can i do it

    If you want to save the file into special location, you could use File class to achieve it. like this:

    string path = @"D:\Data\MyTest.txt";
                try
                {
                    // Delete the file if it exists.
                    if (File.Exists(path))
                    {
                        // Note that no lock is put on the
                        // file and the possibility exists
                        // that another process could do
                        // something with it between
                        // the calls to Exists and Delete.
                        File.Delete(path);
                    }
                    // Create the file.
                    using (FileStream fs = File.Create(path))
                    {
                        Byte[] info = new UTF8Encoding(true).GetBytes("//This is some text in the file.");
                        // Add some information to the file.
                        fs.Write(info, 0, info.Length);
                    }
    
                    // Open the stream and read it back.
                    using (StreamReader sr = File.OpenText(path))
                    {
                        string s = "";
                        while ((s = sr.ReadLine()) != null)
                        {
                            Console.WriteLine(s);
                        }
                    }
                }
    
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 7:43 AM
  • Hi,

    dte.ItemOperations.NewFile is intended to simulate the New File dialog. Normally you don't need that.

    To add a file (ProjectItem) to an existing project, see:

    HOWTO: Create a project item from a Visual Studio add-in
    http://www.visualstudioextensibility.com/articles/add-ins/

    To get the ProjectItem once added, see:

    PRB: Solution.AddXXX and ProjectItems.AddXXX methods return Nothing (null)
    (same link)

    Once you have the ProjectItem, to add code to it see:

    HOWTO: Manipulate a code or form designer file from a Visual Studio add-in
    (same link)






    My portal and blog about VSX: http://www.visualstudioextensibility.com; Twitter: https://twitter.com/VSExtensibility; MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Monday, June 12, 2017 10:24 AM