Write file in C:\\ drive permission issue [closed]

2019-06-13 03:01发布

问题:

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

回答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).