|
I think your best shot is to iterate. There really is no way to get around the fact you are manipulating a set of files when you fail. FileInfo.Attributes will have the flags of the file you point it too.
FileInfo info = new FileInfo(thepathtofile);
info.Attributes &= ~FileAttributes.ReadOnly;
It will remove the Read-Only flag from the file if it is set (and do nothing if it isn't).
Of course this insn't foolproof either. If you don't have permissions to change this on a previlaged filesystem then you'll get an SecurityException toggling the flag.
In general terms, you just have to deal with the fact permissions aren't set correctly. Maybe the correct course of action for when attempting a Directory.Delete where you have Read-Only files is to catch the exception and abort. Altert the user and don't attempt to fix it on your own. There might be a good reason why a Read-Only file is in a directory.
|
|
|
|
|
Hi Tom,
Thanks for the reply. I guess I have to iterate.
The thing is that the user itself creates the read-only file! Let me just tell you a bit more about the whole context. The system is basically a report generator that generates excel files dynamically in a particular folder that the user has read/write access to. However, the user should not be able to generate the report and make manual changes to it (save over it). This is why I chose to create the file read only. Of course, it definitely is not fool proof. The user can always go and remove the read only flag. This is an intranet system and the read only flag is there to avoid accidental overwrites rather than a comprehensive data protection measure.
Thanks again for the reply.
Pankaj
Without struggle, there is no progress
|
|
|
|
|
Dear all,
i have to connect two computes on two seperate phoneline through dial up connection. both computers have Windows 2000 professional installed on them.
by doing this i want to access the remote database(Sql Server 7.0) from one computer to the other. To do this i have dialed from one computer to the other and connection was established, but after some time like 1, 2 or three minutes the connection lost due to some problem which i could not be able to identify. could any anybody help me in this regard that what problem is there.
Thanking you all.....
Muhammad Sheraz Khan Pakistan.
|
|
|
|
|
Sounds like a question for the "Operating Systems/SysAdmin" forum.
|
|
|
|
|
Hello,
Any one who could give me an advice on how to deep copy a Hashtable in as short time as possible?
My first implementation is:
protected void CopyHash(ref Hashtable src, ref Hashtable dst)
{
dst.Clear();
Array copy = Array.CreateInstance(typeof(DictionaryEntry), src.Count);
src.CopyTo(copy,0);
foreach (DictionaryEntry Entry in copy)
{
dst.Add(Entry.Key, Entry.Value);
}
}
Thanks
Rickard
|
|
|
|
|
First of all, you don't need to ref a Hashtable , it's already a reference Type! Second, your method above isn't a deep copy either because it doesn't actually copy the objects stored in the table.
An easy way would be to serialize it to a MemoryStream and deserialize to get the new object. Without an instance manager like .NET Remoting uses, the deserialized objects shouldn't be hooked-up to the ones that they were before. The only stipulation is that the keys and values are serializable, which means they must be attributed with SerializableAttribute , even if they implement ISerializable .
If you wanted to use your code above, make sure that you check each key/value for an impementation of ICloneable and call Clone when possible.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks!
My new implementation:
protected void CopyHash(ref Hashtable src, ref Hashtable dst)
{
MemoryStream ms= new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
//serialize source to a stream
bf.Serialize(ms,src);
//deserialize it to the copy
ms.Seek(0,0);
dst = (Hashtable)bf.Deserialize(ms);
}
A comment: I had to ref the destination Hashtable otherwise the result of the function was an empty Hashtable.
Rickard
|
|
|
|
|
You don't need the ref on the input Hashtable , though, which is what I was trying to get at. And actually, since the deserialization process creates a new Hashtable , you'd be better off using out for the second parameter, that way you don't have to initialize an extraneous instance of a Hashtable that's going to be a new instance after deserialization anyway. So your method signature would look like this:
protected void CopyHash(Hashtable src, out Hashtable dst);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
OleDbConnection conn = new OleDbConnection(string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Mode=ReadWrite",txtDbPath.Text));
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT Name FROM MSysObjects WHERE Type = 1";
OleDbDataReader reader = null;
try
{
conn.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
lstTables.Items.Add (reader.GetName(0).ToString());
}
}
catch (Exception ex)
{
// Providing something like this in your ...(continued)
Console.Error.WriteLine("An error occured: {0}", ex.Message);
MessageBox.Show(ex.Message.ToString());
}
finally
{
if (reader != null)
reader.Close();
conn.Close();
}
Error is
An error occured: Record(s) cannot be read; no read permission on 'MSysObjects'.
|
|
|
|
|
try:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=Admin;Password=
or maybe with double quotes on the password, but i dont think you need them, just let it end at the =
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=Admin;Password=""
or with all default options:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User Id=Admin;Password="";Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
|
|
|
|
|
I ran into a problem that I couldn't see how to solve in C#.
I have a method that takes a Control parameter. I wanted to further restrict it so that is only took Control objects that also implemented a particular interface. So I'd like to be able to declare something like:
public interface IMyNotificationsControl : Control
{
void MyNotification1();
void MyNotification2();
}
Of course, this fails at compilation, since Control is an object, not an interface.
If it were permitted, it would mean that an object of this type would support the entire Control interface (in fact derived from it) but also implement the additional methods.
That way, I could declare my function like this:
public void MyFunction(IMyNotificationsControl control)
{
...
}
Of course, you could derive a new object type from Control to accomplish this, but in my case, I need to take a wide variety of Control types (e.g. different classes derived from specific controls like TreeView etc.
I realise that multiple inheritance would solve this problem. But is there a better way to deal with it in C# other than casting and trusting the programmer?
Tom Clement
|
|
|
|
|
The only way I can see is:
public interface IMyNotificationsControl
{
...
}
public void MyFunction(IMyNotificationsControl control)
{
if( ! ( control is System.Windows.Forms.Control ) )
{
throw new Exception( "Must be of type System.Windows.Forms.Control" );
}
...
}
or
public void MyFunction(System.Windows.Forms.Control control)
{
if( ! ( control is IMyNotificationsControl ) )
{
throw new Exception( "Must implement IMyNotificationsControl" );
}
...
}
sorry, i dont think there is any other way.
|
|
|
|
|
Unless you create a new abstract class that derives from Control and implements the interface (stubbing out the interface methods as abstract methods), you can't automatically get what you want.
You say that you have to handle a wide variety of controls including TreeView, so you will be dealing with Controls that don't implement the interface, correct?
If this is the case, your method should do something like this:
public void MyFunction(Control control)
{
IMyNotificationsControl imnc = control as IMyNotificationsControl;
if (imnc != null)
{
}
}
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Thanks for the reply Kentamanos (and krisp). Both solutions are reasonable. I actually used krisps, since I had to fail if the control did not implement the interface. The discussion is really a theoretical one.
My point was that I couldn't just derive from Control and include the interface methods, since, for example, I'd need to handle controls derived from both (e.g.) ListView and TreeView. Since I can't insert my new control in the object hierarchy above these two controls, given the single inheritance nature of things in C# I couldn't see the kind of solution I'd really like, which is the ability to declare a Type consisting of a class plus an interface.
I guess there could be a restricted interface construct that would provide something more than what C# currently provides but that was less then full multiple interitance. This would be something like:
interface IMyInterface RestrictedTo <objecttype1>, <objecttype2>
Oh well...
Tom
|
|
|
|
|
Another solution could be to have interface with a method that returns Control
interface IMyIFace
{
... methods ...
Control GetControl();
}
that way, when you inherit from some control and implement IMyIFace, you will return this in GetControl() method.
Then in the methods that accepts IMyIFace, you can get a hold of control by calling GetControl(),
and you'll have guarantee that the passed object implements IMyIFace and that it can give you control.
Of course, then it's up to the implementator to make sure they return this in GetControl()
|
|
|
|
|
Anyone have any ideas on how to store richtext in an access database; then retreive the data and reload the richtext control without losing the formatting?
I've spent days looking on the web for answers and so far came up empty.
Any help will be greatly appreciated...
|
|
|
|
|
I've never done this, but what occurs to me is to turn the content of the control into a bunch of bytes and then write the content of the control into the database as a blob. Of course, there could be a simpler more direct route...
|
|
|
|
|
Just store it in a Memo field, and use the RichTextBox.Rtf property, which you could've found in seconds if you used the .NET Framework SDK documentation instead of days searching on the web.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
Your suggestion seemed to work. Thanks!. I used
s = txtData.Rtf.ToString ();
to save the data in a string and then
txtData.Rtf = s;
to put the data back in a richtext control.
In trying to move it to an MS Access memo control I used the following:
string x = @"UPDATE notes SET notetext = " +
'"' +
txtData.Rtf +
'"' +
" WHERE notekey = 1";
When I try to update the record I get a SQL error. I think the string is not getting moved properly in x. SQL gives me the following error:
Message = Syntax error in string in query expression '"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Trebuchet MS;}}
\viewkind4\uc1\pard\f0\fs24 TEST\par
}'.
I'm new to this so I'm feeling pretty dumb here. Any ideas on what I can do to make this work.
Thanks,
Bob
|
|
|
|
|
First of all, you don't need to use RichTextBox.Rtf.ToString - it already is a string.
Second, the error in your query expression is because you're not encoding the string. If you use parameterized queries, you'll find this is taken care of for you and is a much better design, especially when you have to do batch updates:
OleDbConnection conn = new OleDbConnection("your connection string");
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "UPDATE notes SET notetext = @notetext WHERE notekey = 1";
OleDbParameter notetextParam = cmd.Parameters.Add("@notetext",
OleDbType.LongVarWChar, 4000);
try
{
notetextParam.Value = txtData.Rtf;
cmd.ExecuteNonQuery();
}
finally
{
if (conn != null && (conn.State & ConnectionState.Open) != 0)
conn.Close();
} With ADO.NET, do not use string concatentation to avoid problems like this. Using parameterized queries are far more robust and provide many other features like being able to use an OleDbCommandBuilder to create INSERT, UPDATE, and DELETE statements that correspond to a provided simple SELECT statement, and more.
Notice also that I did not include my parameter in quotes in the query string. ADO.NET will take care of this as well.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks a million. Your suggestions worked perfectly.
Bob
|
|
|
|
|
Just wondering,
Why, in a child class, can you not set an abstract method you override, to call a different method that returns something else, but has the same signature. And declare the abstract method by Explicit means.
I know it is a compile error, but is it not something they could have easily allowed, or are there behind the scenes issues im not understanding?
You can do this with interfaces, so why not abstract methods?
public class BaseClass
{
protected abstract string MyMethod();
}
public class ChildClass : BaseClass
{
private int _MyInt = 6;
protected override string BaseClass.MyMethod()
{
return _MyInt.ToString();
}
public int MyMethod()
{
return _MyInt;
}
}
|
|
|
|
|
Few comments...
Classes with abstract methods have to be marked abstract. Think about it. You can't instantiate one because it doesn't know how to implement the abstract method, so the class becomes abstract.
You can never define two functions with the same name that take the same parameters but return different types. This class would never compile (has nothing to do with abstract classes):
<div class="code" style="font-family:Courier New;font-size:10pt;background-color:#FFFFFF;">
<span style="color:#0000FF;">public class </span><span style="color:#000000;">test<br>
{<br>
</span><span style="color:#0000FF;">string </span><span style="color:#000000;">Fred()<br>
{<br>
</span><span style="color:#0000FF;">return </span><span style="color:#000000;">"one";<br>
}<br>
<br>
</span><span style="color:#0000FF;">int </span><span style="color:#000000;">Fred()<br>
{<br>
</span><span style="color:#0000FF;">return </span><span style="color:#000000;">1;<br>
}<br>
}<br>
</span>
</div>
You probably know, but the second class should look like this:
<div class="code" style="font-family:Courier New;font-size:10pt;background-color:#FFFFFF;">
<span style="color:#0000FF;">public class </span><span style="color:#000000;">ChildClass : BaseClass<br>
{<br>
</span><span style="color:#0000FF;">private int </span><span style="color:#000000;">_MyInt = 6;<br>
<br>
</span><span style="color:#0000FF;">protected override string </span><span style="color:#000000;">MyMethod()<br>
{<br>
</span><span style="color:#0000FF;">return </span><span style="color:#000000;">_MyInt.ToString();<br>
}<br>
} <br>
</span>
</div>
Is there something else I'm missing on this?
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
I was getting at the fact that you can do this with interfaces, so why not abstract classes?
Seems like it should be logical, an abstract class is just like a regular class with the abstract methods comming from an interface, except it has the abstract keyword.
public interface BaseInterface
{
string MyMethod();
}
public class ChildClass : BaseInterface
{
private int _MyInt = 6;
string BaseInterface.MyMethod()
{
return MyMethod().ToString(); // this calls int MyMethod()
}
public int MyMethod()
{
return _MyInt;
}
}
|
|
|
|
|
I see what you're saying now.
In order for you to call the ChildClass's "version" of the method (not the one that he implements to fulfill his derivation from the abstract class), you'd have to have some sort of syntax for specifying which version you were calling.
In the case of the interfaces, it's really a function of casting to the inteface and calling the method. It doesn't make sense to cast an instance of the the derived class to the derived class to get the derived one's "personal" version.
I'm sure this syntactical stuff (which is fairly nasty) was purposely avoided. That's one good reason to not allow multiple inheritance (which is a design descision MS made with C#).
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|