1

I am getting below error when I am trying to convert xsd to cs using C# code

Error details
Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 4.0.30319.17929] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\abc.cs'. Error: Error generating classes for schema 'C:\test\safe'. Access to the path 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\abc.cs' is denied.

Code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Process p = new Process();
        string xsdFile = "c:\\test\\abc.xsd";

        try
        {
            if (!File.Exists(xsdFile))
            {
                Console.WriteLine("Error. File doesn't exists.");
                Environment.Exit(1);
            }
            String pass = "********";
            p.StartInfo.UserName = "asdasd";
            p.StartInfo.Domain = "asdasd";
            SecureString passWord = new SecureString();
            foreach (char c in pass.ToCharArray())
            {
                passWord.AppendChar(c);
            }

           p.StartInfo.Password = passWord;
            p.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\XSD.exe";
             p.StartInfo.UseShellExecute = false;
              p.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin";
              p.StartInfo.Arguments = "/c " + xsdFile;

            p.Start();
            Console.WriteLine(p.StartInfo.WorkingDirectory);
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(p.StartInfo.WorkingDirectory);
            Console.ReadLine();
        }
    }
}
}
M4N
  • 94,805
  • 45
  • 217
  • 260
Shah Jigesh
  • 51
  • 11
  • have you used the debugger.. come on now.. step through the code and inspect the path as well as file name to see if it exist..! – MethodMan Sep 10 '15 at 21:26

1 Answers1

0

Just as it says... Access is denied. Start with this: Manually clean and build your app in "Release" mode. Navigate to your "Release" folder and run your EXE as Administrator. If this works you simply need to ensure that the user (probably yourself) that is running the application has full read/write access to "C:\Program Files (x86)\Microsoft Visual Studio 11.0\" You may also use this link: How to start a Process as administrator mode in C# to help you automatically run the New Process as Administrator.

This could also simply be an error in what you are expecting:

From what you have above I am assuming this is not your expected output directory, You should consider using the "/outputdir:directory" switch for XSD.exe

Community
  • 1
  • 1
spraus
  • 9
  • 1
  • 5