Why does it required UAC for changing files?

354 views Asked by At

I creted an application that has ini file and db.mdb (access) and it downloads some images from web to a folder that is near of App.

Well, I created a setup file with Setup Factory Software. Everything seems quite fine and I started my application and it doesnt change ini file's value, doesnt download images and doesnt insert any recort to db.

here is the path of my app , C:\Program Files (x86)\XXXXXXX

here is code of instering row to db

public void AddChannels(List<MediaChannel> list)
{

 string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=data\\db.mdb;";
 try
 {
  using (OleDbConnection connection = new OleDbConnection(connectionString))
  {
   OleDbCommand cmd = new OleDbCommand();
   cmd.Connection = connection;

   connection.Open();


   int tmp1;
   string tmpstr;
   foreach (var mediaChannel in list)
   {
    tmp1 = mediaChannel.ImagePath.LastIndexOf('/');
    tmpstr = mediaChannel.ImagePath.Substring(tmp1+ 1, mediaChannel.ImagePath.Length - tmp1 - 1);

    cmd.CommandText = "Insert Into Channels(ChannelName,CategoryName,Url,ImagePath,ChannelType) values(@ChannelName,@CategoryName,@Url,@ImagePath,@ChannelType)";
       cmd.Parameters.AddWithValue("@ChannelName", mediaChannel.Name);
       cmd.Parameters.AddWithValue("@CategoryName", mediaChannel.CategoryName);
       cmd.Parameters.AddWithValue("@Url", mediaChannel.Url);
       cmd.Parameters.AddWithValue("@ImagePath", tmpstr);
       cmd.Parameters.AddWithValue("@ChannelType", (int)mediaChannel.ChannelType);
       cmd.ExecuteNonQuery();
      cmd.Parameters.Clear();
   }
  connection.Close();
 }
} 
catch (Exception ex)
{

throw new Exception(ex.Message);
}
}

and here is my image downloading method

private void DownloadLogo()
{

string localFilename = Application.StartupPath + @"\Imgs\";
if (!Directory.Exists(localFilename))
Directory.CreateDirectory(localFilename);

foreach (var mediaChannel in channelList)
{

string imgName = mediaChannel.ImagePath;
if (imgName.Contains("http://"))
{
int tmp1 = mediaChannel.ImagePath.LastIndexOf('/');
imgName = mediaChannel.ImagePath.Substring(tmp1 + 1, mediaChannel.ImagePath.Length - tmp1 - 1);

}


if (!File.Exists(localFilename + imgName))
{


using (var client = new WebClient())
{

client.DownloadFile(mediaChannel.ImagePath, localFilename + imgName);
}
}
}
}

Everything looks OK and I have never get this kind of issue. If I run my program as administrator, it works...

One more thing, I opened my db that is in C:\Program Files (x86)\XXXXXXX\data I used MS Access and interesting, I can not edit table. It says me you need to save as your file for changing.. I think program files (86) is protected by UAC. My OS is Windows 8 Professional.

Maybe setup program makes it like that? Because I cant use visual studio setup because of limited license of install shield setup then I used 3rd part setup maker app.

So, How to solve this problem? My customer is waiting for me to fix this issue..

3

There are 3 answers

8
Radenko Zec On BEST ANSWER

You probably need to use AppData folder instead of Program Files folder. Check this similar question Saving a file to Application Data in c#

0
ertan2002 On

Finally I solved what was the problem.. I gave my files permissions to write and everything is OK.

http://www.indigorose.com/webhelp/suf9/Program_Reference/Actions/File.SetPermissions.htm

1
Jon Skeet On

You're trying to modify a file within Program Files. That's an area of the file system which isn't designed for mutable application data - it's basically meant to be read-only. Modifications to this area are suspect, hence the UAC prompt.

You should fix this by storing your data in a more appropriate place in the file system. Exactly where that is will depend on what your application is trying to do. (Is it a service, or just a local client app? Is the data meant to be specific to the logged-in user, or computer-wide?)