0

i have developed a win application with c# which generate file in a folder in c:\ drive. when try to generate file there then problem occur for permission issue but when the application generate file in other drive than C:\ then no problem occur. so when i will distribute my apps setup to end user then i wont be sure that user who will install my apps does has the permission to generate file in C:\ drive.

so guide me how can i overcome this issue. should i Using Manifests to Elevate an application in win OS?

i got some article

http://blogs.msdn.com/b/nikhiln/archive/2007/04/19/embed-a-manifest-to-make-an-application-elevate-in-vista.aspx

http://www.codeproject.com/Articles/105506/Getting-Elevated-Privileges-on-Demand-using-C

http://archive.msdn.microsoft.com/KB981778

etc......please guide me with right knowledge. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • 4
    You do not want to elevate the permissions unless absolutely necessary. There is a reason why Windows doesn't allow just any program to write directly to the C: drive. Instead, use the folder that was designated for that purpose, C:\ProgramData. – dub stylee May 15 '14 at 18:57
  • http://stackoverflow.com/questions/8141795/how-to-add-an-assembly-manifest-to-a-net-executable/8844086#8844086 for VS2005 http://support.microsoft.com/kb/944276/en-us – Mou May 15 '14 at 19:04
  • there is no folder called programdata rather there is folder called program files. – Mou May 15 '14 at 19:05
  • If you are using Windows XP then it would be instead C:\Documents and Settings\All Users\Application Data. In Windows 7/8 it is C:\ProgramData. – dub stylee May 15 '14 at 19:49

1 Answers1

1

You can start your application again with elevated permission and have some check at the application's start to see if this is the case. Here's an example: (Be careful not to get into an endless loop of the application starting itself.)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        string[] args = Environment.GetCommandLineArgs();
        if (args.Length > 1 && args[1] == "-e") Text = "Elevated";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Process process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = Application.ExecutablePath,
                Arguments = "-e",
                Verb = "runas",//-Admin.
            }
        };
        process.Start();
    }
}

I agree, though, that storing information in "C:" is probably not a good idea. You can try someplace like: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).

ispiro
  • 26,556
  • 38
  • 136
  • 291