setting up filesystem permissions - c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string Directoryd = "D:";
string mydirectory = Directoryd + "\\" + "rs\\";
if (!Directory.Exists(mydirectory))
{
Directory.CreateDirectory(mydirectory);
}
DirectoryInfo di = new DirectoryInfo(mydirectory);
DirectorySecurity ds = di.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(#"*",FileSystemRights.FullControl,AccessControlType.Allow));
di.SetAccessControl(ds);
}
}
}
this is my code when i execute this the pop up is showing that
Actually, this code is to create a folder rs and set its permission to deny full control but on running the error come with the message
Some or all identity references could not be translated.
What is the error?

You should change the following line:
ds.AddAccessRule(new FileSystemAccessRule(#"*",FileSystemRights.FullControl,AccessControlType.Allow));
to:
ds.AddAccessRule(new FileSystemAccessRule(#"Everyone",FileSystemRights.FullControl,AccessControlType.Allow));
Also if you look at the following Everyone Group there is an answer a bit further down that suggests you should use SSID's instead of the names.

Try the group "Everyone", not *.

Related

Trying to transfer files for the target user

today we were trying to transfer files with using C# but there are some problems here i don't really know what is wrong
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace FileTransfer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
var userDir = new DirectoryInfo(Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile));
string path = curDir + #"\1.jar";
string path2 = userDir + #"\Desktop\2.jar";
File.Move(path, path2);
}
}
}
It says 1.jar file cannot found.
Try this:
string path = Path.Combine(curDir,"1.jar");
string path2 = Path.Combine(userDir,"Desktop","2.jar");
//if this still doesn't work, put a breakpoint after these two lines
When your breakpoint is hit look at the value of path. Is there a even file at that location? If not, you need build your path correctly.
Also, it would be a good idea to check if the file exists before you try to move it:
if(!File.Exists(path))
{
//handle this scenario here
return;
}
File.Move(path,path2);

FileSystemWatcher.Created is not working properly

I want to monitor some directory using FileSystemWatcher but FileSystemWatcher.created is not working at all .
CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Threading;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static string fileName;
static string dirFileName;
static void Main(string[] args)
{
string _Dir = ConfigurationManager.AppSettings["Dir"];
Console.WriteLine("MAIDProcessFileTest processing started");
FileSystemWatcher _watch = new FileSystemWatcher();
_watch.Path = _Dir;
_watch.Created += FileCreated;
Console.ReadLine();
}
private static void FileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Before is treat");
fileName = Path.GetFileName(e.FullPath);
dirFileName = Path.GetDirectoryName(e.FullPath) + #"\" + Path.GetFileName(e.FullPath);
}
}
}
Can anybody tell me that why this code is not working ... whenever I debug _watch.Created += FileCreated; is not working at all. where is the error can anybody tell me . Please Help
You need to start it
_watch.EnableRaisingEvents = true;
FileSystemWatcher.EnableRaisingEvents Property
You should probably start here though
FileSystemWatcher Class
It has a great example, and everything you need to know to use it

Charset of executed

I have a problem with my project.
I have a js function sig7 which generates me hash.
But the charset of html page is required to be utf-8.
I try to execute this js in my C# app with library
Everything is OK, but generated hash is not valid. I think it happens because the charset of code isn't utf-8.
My app:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Noesis.Javascript;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader("c://lib.js");
string script = sr.ReadToEnd();
JavascriptContext context = new JavascriptContext();
context.Run(script);
var sig = context.Run(#"sig7('057d194dd856d4f8cd15bc85fbbddbf136df809f72f5a435e6af41c1f82f950f,Id166084073,ldsclldscl');").ToString();
richTextBox1.Text = sig;
}
}
}
sig must be 1dbde1d62b6e0dca7d56f72e50c178132e236978d2dba1e2c3ada5b7b8973aeb but not 7211ed8fe85d750c26cb5abb499bfa88b7cecbe968fc6cc7efa69dc496c8ae12 which is generated by my app.
Any help?
Should this line:
var sig = context.Run(#"sig7('057d194dd856d4f8cd15bc85fbbddbf136df809f72f5a435e6af41c1f82f950f,Id166084073,ldsclldscl');").ToString();
Be:
var sig = context.Run(#"sig7('057d194dd856d4f8cd15bc85fbbddbf136df809f72f5a435e6af41c1f82f950f', 'Id166084073', 'ldsclldscl');").ToString()
?

Why doesn't my code-behind execute?

I am trying to output the value of x in the following request: http://localhost:4827/Default.aspx?x=123123
I am getting no output at all.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using MPads.Common;
using MPads.WebPatientPortal.Interfaces;
using MPads.WebPatientPortal.Presenters;
using MPads.BusinessLogic.Interfaces;
using MPads.Components.Interfaces;
using MPads.DataAccess;
using MPads.DataAccess.Interfaces;
using System.Text;
using System.Diagnostics;
namespace MPads.WebPatientPortal
{
public partial class Default : System.Web.UI.Page, IDefault
{
private DefaultPresenter _presenter;
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
_presenter = new DefaultPresenter();
var fields = Request.QueryString;
if (fields.Count > 0)
{
var x = Utilities.GetNvpValue(fields, "x").ToString();
Trace.Write(x);
}
}
public void loadAjaxContent(string content)
{
Page.RegisterStartupScript("myScript", " <script type=\"text/javascript\"> loadAjaxContent('" + content + "','POST',null); </script>");
}
public void DisplayMessage(string message)
{
}
}
}
Do you have your even handler set up in your ASPX?
Assuming that you do: Consider instead of Trace.Write(); using Response.Write();
Trace.Write(); writes to a trace listener (which you can have display at the bottom of your page).
Response.Write(); outputs text results directly to your page, not through a Trace Listener.
Use Glimpse to find out this, here's the link Glimpse-A client side Glimpse to your server

Registry Problem

I made a launcher for my game server. (World of Warcraft)
I want to get the installpath of the game, browsed by the user.
I'm using this code to browse, and get the installpath, then set some other strings from the installpath string, then just strore in my registry key.
using System;
using System.Drawing;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.IO;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using System.Linq;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string InstallPath, WoWExe, PatchPath;
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey LocalMachineKey_Existence;
MessageBox.Show("Browse your install location.", "Select Wow.exe");
OpenFileDialog BrowseInstallPath = new OpenFileDialog();
BrowseInstallPath.Filter = "wow.exe|*.exe";
if (BrowseInstallPath.ShowDialog() == DialogResult.OK)
{
InstallPath = System.IO.Path.GetDirectoryName(BrowseInstallPath.FileName);
WoWExe = InstallPath + "\\wow.exe";
PatchPath = InstallPath + "\\Data\\";
LocalMachineKey_Existence = Registry.LocalMachine.CreateSubKey(#"SOFTWARE\ExistenceWoW");
LocalMachineKey_Existence.SetValue("InstallPathLocation", InstallPath);
LocalMachineKey_Existence.SetValue("PatchPathLocation", PatchPath);
LocalMachineKey_Existence.SetValue("WoWExeLocation", WoWExe);
}
}
}
}
The problem is:
On some computer, it doesnt stores like it should be. For example, your wow.exe is in C:\ASD\wow.exe, your select it with the browse windows, then the program should store it in the Existence registry key as C:\ASD\Data\ but it stores like this:
C:\ASDData , so it forgots a backslash :S
Look at this picture:
http://img21.imageshack.us/img21/2829/regedita.jpg
My program works cool on my PC, and on my friends pc, but on some pc this "bug" comes out :S
I have windows 7, with .NEt 3.5
Please help me.
Can you debug and see what InstallPath contains?
Try it with Path.Combine instead of string concatenation, e.g.:
WowExe = Path.Combine(InstallPath, "wow.exe");
PatchPath = Path.Combine(InstallPath, #"\Data\");

Categories

Resources