file deletable?, shortcuts, .mdb file backup

D

David Krmpotic

Hi All!!


Could you please help me with three problems:

1.
I would like to have a procedure that returned true if the file is
writtable=deletable.. I tried everything but so far the only way I can
really tell if the file can be delete is to actually try to delete it and
catch an exception.. Trying to open a file with StreamReader in write mode
didn't work as expected.

2:
I know how to create shortcut, using COM API, but I can't seem to find
parameter for setting shortcuts name when put for example on Start Meni.. if
I don't set this, then the name of link is just the name of file, which is a
little bit ugly.

my code is here.. note the line, marked with ###

public static void CreateShortcut(string name, string shortcut_dir, string
app_path, string description)
{
// Get the app path and filename
//string app = app_path;
try
{
// Create a Windows Script Host Shell class
IWshShell_Class shell = new IWshShell_ClassClass();

//get application executable from entire path
string app_file = Path.GetFileName(app_path);

// Define the shortcut file
IWshShortcut_Class shortcut = shell.CreateShortcut(shortcut_dir + "\\" +
app_file + ".lnk") as
IWshShortcut_Class;

// Set all its properties
//### I thought the following line was what I was looking for.. but when
I tried to set this,
//compiler says it is only readonly field.. so.. my question is: is this
the field and this is the bug, or should I do something else to set the real
name of the shortcut which user sees?
//shortcut.FullName = name;
shortcut.WorkingDirectory = Path.GetDirectoryName(app_path);
shortcut.TargetPath = app_path;
shortcut.IconLocation = app_path + ",0";
if(description != null)
{
shortcut.Description = description;
}

// Save it
shortcut.Save();
}
catch(COMException ex)
{
Console.WriteLine(ex.Message);
}
}

3.
I would like to periodically make a backup copy of all .mdb files while the
application is running in the background (it is database server .. c#
remoting, ADO.NET).
The thing is that not all the recent changes are in those .mdb files, when I
copy them.. if I reset the server, then the files are up-to-date.
I see the solution in two possible ways:
somehow get all the data from ADO.NET objects and create new mdb files.
or..somehow forcing ADO, to write all the pending actions before physically
copying .mdb files.


I am currently traveling, so my access to internet, book and other
information is restricted.. I've been fighting with those problems for some
time, but then I came to blind alley. Your help would be greatly
appreciated.


Thank you so much!!!

David Krmpotic
 
M

Maqsood Ahmed

For your first question, you can check that if a file is ReadOnly or
not.

System.IO.FileInfo fi = new FileInfo("yourFilePathgoesHere");
if((fi.Attribute & System.FileAttributes.ReadOnly) ==
System.FileAttributes.ReadOnly) //If File is readonly
//Don't delete.

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net
 
D

David Krmpotic

Thank you for response.. but I am afraid that this doesn't work for me.. I
think I didn't express myself clearly. I needed a procedure that would tell
me if the file is in use and thus not writable/deletable.

I tried your recommendation, but file's ReadOnly flag is not set when file
is in use.

I just found other easy solution:

public static bool IsFileInUse(string file)
{
try
{
FileStream fs = File.OpenWrite(file);
fs.Close();
return false;
}
catch
{
return true;
}
}

I was close before, but I used OpenRead instead of OpenWrite. The problem
was that you can open the file for reading although it's already open for
reading somewhere else (and thus not deletable). But you can only open the
file for writing it absolutely nothing is currently accessing it.


Thank you !!

PS: do you have any ideas about my second two questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top